Notification和NotificationManageride
1.Broadcast Receiver組件沒有提供可視化的界面來顯示廣播信息。這裏咱們能夠使用Notification和NotificationManager來實現可視化的信息顯示。經過使用它們咱們能夠顯示廣播信息的內容,圖標
this
以及震動等信息。spa
2.使用Notification和NotificationManager也比較簡單,通常得到系統級的服務NotificationManager,而後實例化Notification,設置其屬性,經過NotificationManager發出通知就能夠了。code
基本步驟以下:xml
1)得到系統級的服務NotificationManager,這裏比較簡單,經過Context.getSystemService()方法便可實現。
對象
1 String service = Notification; 2 NotificationManager nm = (NotificationManager)getSystemService(service);
2)實例化Notification對象,並設置其屬性。blog
//實例化Notification Notification n = new Notification(); //誰知顯示圖標,圖標會在狀態欄顯示 int icon = n.icon = R.drawable.icon; //設置顯示提示信息, String tickrtText = "Test Notification"; //顯示時間 long when = System.currentTimeMills(); n.icon = icon; n.tickerText = tickerText; n.when = when;
3)調用setLatestEcentInfo()方法在視圖中設置圖標和時間事件
//實例化Intentget
Intent intent = new Intent(this,MainActivity.class); //得到 PendingIntent PendingIntent pi = PendingIntent .getActivity(this,0,intent,0); //設置時間信息 n.setLastEventInfo(this,"My content" , "My Content" , pi);
4)發出通知源碼
//表示該通知的ID int ID = 1; //發出通知 nm.notify(ID,n);
3.下面直接經過一個例子來講明
MainActivity:
1 public class MainActivity extends Activity { 2 3 private Button btn_send; 4 String MY_ACTION = "com.example.notification.MY_ACTION"; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 btn_send = (Button) findViewById(R.id.btn_send); 12 btn_send.setOnClickListener(new Button.OnClickListener() { 13 14 @Override 15 public void onClick(View v) { 16 Intent i = new Intent(); 17 i.putExtra("message","接收到消息"); 18 i.setAction(MY_ACTION); 19 sendBroadcast(i); 20 } 21 }); 22 23 } 24 }
MyBroadCast(必定記住在manifest.xml中申明broadcast):
1 public class myBroadCast extends BroadcastReceiver { 2 3 public myBroadCast() { 4 } 5 6 @Override 7 public void onReceive(Context context, Intent intent) { 8 9 Intent i = new Intent(); 10 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 11 Log.e("message", intent.getStringExtra("message")); 12 i.putExtra("message", intent.getStringExtra("message")); 13 i.setClass(context, SecondActivity.class); 14 context.startActivity(i); 15 16 } 17 }
SecondActivity:
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_second); 4 btnStop = (Button) findViewById(R.id.btn_cancel); 5 tvShow = (TextView) findViewById(R.id.tv_showText); 6 nm = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 7 8 notification = new Notification(); 9 notification.icon = R.drawable.ic_launcher; 10 notification.defaults |= Notification.DEFAULT_SOUND; 11 long when = System.currentTimeMillis(); 12 String tickerText = "Test Notification"; 13 notification.tickerText = tickerText; 14 notification.when = when; 15 // 實例化Intent 16 Intent intent = new Intent(this, MainActivity.class); // 點擊通知後要轉向的界面 17 // 得到pendingIntent 18 PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 19 // 設置事件信息 20 notification.setLatestEventInfo(this, "My titlk", "My content", pi); 21 22 new Thread() { 23 @Override 24 public void run() { 25 while (true) { 26 try { 27 Thread.sleep(1000); 28 handler.sendEmptyMessage(0); 29 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 35 } 36 } 37 38 }.start(); 39 40 btnStop.setOnClickListener(new Button.OnClickListener() { 41 42 @Override 43 public void onClick(View v) { 44 nm.cancel(1); 45 } 46 }); 47 48 } 49 50 Handler handler = new Handler() { 51 @Override 52 public void handleMessage(Message msg) { 53 nm.notify(1, notification); 54 55 } 56 57 }; 58 }