欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)

 更新時(shí)間:2016年12月12日 08:23:30   作者:潘侯爺  
本篇文章主要介紹了Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例,具有一定的參考價(jià)值,需要的朋友可以了解一下。

示例一:實(shí)現(xiàn)通知欄管理

當(dāng)針對相同類型的事件多次發(fā)出通知,作為開發(fā)者,應(yīng)該避免使用全新的通知,這時(shí)就應(yīng)該考慮更新之前通知欄的一些值來達(dá)到提醒用戶的目的。例如我們手機(jī)的短信系統(tǒng),當(dāng)不斷有新消息傳來時(shí),我們的通知欄僅僅是更改傳來短信的數(shù)目,而不是對每條短信單獨(dú)做一個(gè)通知欄用于提示。

修改通知

可以設(shè)置一條通知,當(dāng)然可以更新一條通知,我們通過在調(diào)用NotificationManager.notify(ID, notification)時(shí)所使用的ID來更新它。為了更新你之前發(fā)布的通知,你需要更新或者創(chuàng)建一個(gè)NotificationCompat.Builder對象,從之前的通知中創(chuàng)建一個(gè)Notification對象,然后發(fā)布它使用你之前使用的ID。如果之前的通知仍然可見,系統(tǒng)將直接更新通知的內(nèi)容,如果之前的通知不可見了,一條新的通知將被創(chuàng)建。

下面的代碼演示了更新,以反映已發(fā)生的事件數(shù)量的通知。 它疊加通知,顯示的摘要:

(注意演示,通知數(shù)量會(huì)累積而且點(diǎn)擊通知后通知欄消失)

這里我們不再演示點(diǎn)擊按鈕以及跳轉(zhuǎn)頁面的布局文件,直接上java實(shí)現(xiàn)代碼:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
  private static final int NO_1 = 0x1;
  int num =1;//初始通知數(shù)量為1
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  //按鈕點(diǎn)擊事件(通知欄)
  public void show1(View v){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("新消息");
    builder.setContentText("你有一條新的消息");
    builder.setNumber(num++);
    //設(shè)置點(diǎn)擊通知跳轉(zhuǎn)頁面后,通知消失
    builder.setAutoCancel(true);
    Intent intent = new Intent(this,Main2Activity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pi);
    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NO_1,notification);
  }
}

當(dāng)我們設(shè)置setAutoCancel()為false時(shí),顯示效果如下(點(diǎn)擊后通知欄不消失):

示例二:自定義通知欄

通知的框架允許你去自定義通知的布局。通過RemoteViews對象來定義通知的外觀。自定義通知布局與常規(guī)通知相似,但是它是基于定義在XML文件的RemoteViews對象來操作的。

自定義通知的可用高度是取決于通知視圖的。正常的視圖布局高度限制在64dp,可展開視圖的布局高度限制在256dp。

為了去定義自己的通知布局,從擴(kuò)充XML文件獲取一個(gè)RemoteViews對象的實(shí)例開始。然后,類似于調(diào)用setContentTitle()方法一樣,我們需要調(diào)用setContent()。為了能設(shè)置更多細(xì)節(jié),我們使用RemoteViews對象的方法來設(shè)置更多的內(nèi)容。

1.創(chuàng)建一個(gè)單獨(dú)的XML文件,用來定義通知的布局。你可以使用任何你想用的名字,但后綴必須是.xml。

2.在應(yīng)用里面,使用RemoteViews對象的方法來給你的通知設(shè)置文本和圖標(biāo),通過調(diào)用setContent()把你的RemoteViews對象放到NotificationCompat.Builder里面。避免使用背景圖像,因?yàn)槟愕奈谋究赡軙?huì)變得不太好閱讀。

RemoteViews對象也包含一些方法用來給你去添加Chronometer和ProgressBar。想要了解更多自定義通知條布局的事情,參考RemoteViews的文檔。

注意:當(dāng)你使用自定義的通知條的時(shí)候,特別要注意你自定義的通知條在不同方向與分辨率的設(shè)備上是如何工作的。當(dāng)然這條建議對所有的視圖布局都很重要。但對通知條來說是尤其重要的,因?yàn)橥ㄖ閷系目丶值挠邢蕖2灰炎约旱耐ㄖ獥l做的太復(fù)雜,確保它的靈活性。

為自定義的通知條文本使用樣式資源(Usingstyle resources for custom notification text)

自定義通知條的時(shí)候總是使用樣式資源去定義文本。通知的背景顏色會(huì)變得與設(shè)備與當(dāng)前版本的android有很大的反差。使用樣式文件能幫你很好的解決這一點(diǎn)。從Android 2.3開始,系統(tǒng)就為標(biāo)準(zhǔn)的通知布局定義了文本的樣式,如果你在Android2.3 以及其更高的版本上使用同樣的樣式,你必須確保你的文本相對于背景是可以看見的。

注意:

Notification的自定義布局是RemoteViews,和其他RemoteViews一樣,在自定義視圖布局文件中,僅支持FrameLayout、LinearLayout、RelativeLayout三種布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些顯示控件,不支持這些類的子類或Android提供的其他控件。否則會(huì)引起ClassNotFoundException異常。

帶按鈕的布局相應(yīng)點(diǎn)擊事件在3.0以下版本不啟作用。

下面簡單演示自定義音樂播放notification(未設(shè)置setongoing常駐):

Layout中message.xml(自定義布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center">
  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="仗劍走天涯"/>
  <Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="播放"/>
  <Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="下一首"/>
</LinearLayout>

MainActivity對應(yīng)java實(shí)現(xiàn)代碼的MainActivity.java(只演示點(diǎn)擊事件):

public void show2(View v){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.guojia);
    RemoteViews rv = new RemoteViews(getPackageName(),R.layout.message);
    rv.setTextViewText(R.id.tv,"泡沫");//修改自定義View中的歌名
    //修改自定義View中的圖片(兩種方法)
//    rv.setImageViewResource(R.id.iv,R.mipmap.ic_launcher);
    rv.setImageViewBitmap(R.id.iv, BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
    builder.setContent(rv);
    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NO_2,notification);
  }

至此notification相關(guān)知識(shí)點(diǎn)就總結(jié)完了,以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論