android Notification實(shí)例詳解
1.使用Builder模式來(lái)創(chuàng)建
2.必須要設(shè)置一個(gè)smallIcon,還可以設(shè)置setTicker
3.可以設(shè)置 setContentTitle,setContentInfo,setContentText,setWhen
4.可以設(shè)置setDefaults(閃屏,聲音,震動(dòng)),通過(guò)Notification設(shè)置flags(能不能被清除)
5.發(fā)送需要獲取一個(gè)NotificationManager(getSystemService來(lái)獲取);notify(int id,Notification)
6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能訪問(wèn),需要導(dǎo)入v4包,使用notificationCompat兼容類。
自定義通知
使用RemoteViews來(lái)建立一個(gè)布局,然后使用setContent()設(shè)置;
點(diǎn)擊事件使用PendingIntent來(lái)完成
下面是一個(gè)案例


MainActivity類
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clearNotification(View v) { // 通過(guò)代碼來(lái)清除 NO_CLEAR // 清除也需要manager NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 只能清除由本應(yīng)用程序發(fā)出去的通知 manager.cancelAll(); } public void sendNotification(View v) { // 他是 在v4包中的通知,用于16以下版本發(fā)送通知 // NotificationCompat // 通知的創(chuàng)建 Notification.Builder builder = new Notification.Builder(this); // NotificationCompat.Builder builder2 = new NotificationCompat.Builder( // this); // 顯示在通知條上的圖標(biāo) 必須的 builder.setSmallIcon(R.drawable.ic_launcher); // 顯示通知條上的文字 不是必須的 builder.setTicker("您有一條新的消息!!"); // 狀態(tài)欄中的 builder.setContentTitle("大標(biāo)題"); builder.setContentText("文本"); builder.setWhen(System.currentTimeMillis()); builder.setContentInfo("Info"); builder.setDefaults(Notification.DEFAULT_ALL); // 點(diǎn)擊事件使用Pending Intent intent = new Intent(this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pi); // 生成對(duì)象 16API以上,支持低版本需要使用v4包中的notificationCompat Notification notify = builder.build(); // 設(shè)置不能清除 notify.flags = Notification.FLAG_NO_CLEAR; // 如何將通知發(fā)送出去 NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 通知的唯一值,如果id重復(fù),表明是更新一條通知,而不是新建 mananger.notify((int) (Math.random() * 1000), notify); } public void diyNotification(View v) { // 展示在通知上面的視圖 RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout); Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher).setTicker("自定義通知 ") // 布局 .setContent(views).build(); // 使用RemoteViews來(lái)設(shè)置點(diǎn)擊事件 views.setTextColor(R.id.tv, Color.RED); Intent intent = new Intent(this, OneActivity.class); // 音樂(lè)播放是放在Service PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.tv, pi); Intent intent2 = new Intent(this, MainActivity.class); PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.iv, pi2); // 發(fā)送 NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notify.notify(1, notification); }}OneActivity類
public class OneActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("跳轉(zhuǎn)界面"); setContentView(tv); }}activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.lesson8_notification.MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendNotification" android:text="普通的通知" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="clearNotification" android:text="清除所有通知" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="diyNotification" android:text="自定義通知" /></LinearLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /></LinearLayout>
最后記得注冊(cè)activity
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選