android Notification和NotificationManager的使用
Notification和NotificationManager
1.Broadcast Receiver组件没有提供可视化的界面来显示广播信息。这里我们可以使用Notification和NotificationManager来实现可视化的信息显示。通过使用它们我们可以显示广播信息的内容,图标
以及震动等信息。
2.使用Notification和NotificationManager也比较简单,一般获得系统级的服务NotificationManager,然后实例化Notification,设置其属性,通过NotificationManager发出通知就可以了。
基本步骤如下:
1)获得系统级的服务NotificationManager,这里比较简单,通过Context.getSystemService()方法即可实现。
1 String service = Notification; 2 NotificationManager nm = (NotificationManager)getSystemService(service);
2)实例化Notification对象,并设置其属性。
//实例化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()方法在视图中设置图标和时间
//实例化Intent
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 }