標籤:toc option service options system androi when this protect
package com.example.mynotification;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v4.app.NotificationCompat;import android.view.Menu;public class MainActivity extends Activity {public PendingIntent getDefalutIntent(int flags){PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags); return pendingIntent;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentTitle("測試標題")//設定通知欄標題.setContentText("測試內容")//設定通知欄顯示內容.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))//設定通知欄點擊意圖 //.setNumber(number);.setTicker("測試通知來啦")//通知欄首次出如今通知欄,帶上動畫效果.setWhen(System.currentTimeMillis())//通知欄時間。通常是直接用系統的.setPriority(Notification.DEFAULT_ALL)//設定通知欄優先順序//.setAutoCancel(true)//使用者單擊面板後消失.setOngoing(false)//true,設定他為一個進行中的通知。他們通常是用來表示一個背景工作,使用者積極參與(如播放音樂)或以某種方式正在等待,因此//佔用裝置(如一個檔案下載,同步操作。主動網路連接).setDefaults(Notification.DEFAULT_VIBRATE)//向通知加入聲音、閃燈和震動效果的最簡單、最一致的方式是使用當前的使用者預設設定。//使用default屬性。能夠組合 //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 加入聲音 // requires VIBRATE permission .setSmallIcon(R.drawable.ic_launcher);Notification notification = mBuilder.build();notification.flags = Notification.FLAG_ONGOING_EVENT ;notification.flags = Notification.FLAG_NO_CLEAR;//點擊清除的時候不清除Intent intent = new Intent(getApplicationContext(),MainActivity.class);intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);mBuilder.setContentIntent(pendingIntent);mNotificationManager.notify(0,<span style="font-family: Arial, Helvetica, sans-serif;">notification</span><span style="font-family: Arial, Helvetica, sans-serif;">); </span>}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}不知道為什麼我的點擊還是會清除。鬱悶。
android的通知欄的實現