通知栏的使用

2017-06-08  本文已影响0人  我是你森哥哥

1.  public class MainActivity extends Activity {
2.  
3.      private NotificationManager nm;
4.  
5.      @Override
6.      protected void onCreate(Bundle savedInstanceState) {
7.          super.onCreate(savedInstanceState);
8.          setContentView(R.layout.activity_main);
9.          
10.         //[1]获取通知的管理者  
11.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
12.     }
13.     //点击按钮发送通知
14.     public void click1(View v) {
15. 
16.         //链式调用  特点:返回值都是当前对象
17.     /*  Notification noti = new Notification.Builder(this)
18.         .setContentTitle("我是大标题")
19.         .setContentText("我是大标题的内容")
20.         .setSmallIcon(R.drawable.ic_launcher)
21.         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
22.         .build();*/
23.         
24.         // 为了兼容低版本  使用过时的方法 
25.         
26.         Notification noti = new Notification(R.drawable.ic_launcher, "您收到一条通知", System.currentTimeMillis());
27.         //当收到通知的时候 让呼吸灯闪烁 并且有一个系统默认声音  又想震动 
28.         noti.defaults = Notification.DEFAULT_ALL;
29.         //当收到通知后 不让通知删除 
30.         noti.flags = Notification.FLAG_NO_CLEAR;
31.         
32.         //创建意图对象
33.         Intent intent = new Intent();
34.         intent.setAction(Intent.ACTION_CALL);
35.         intent.setData(Uri.parse("tel:"+110));
36.         //延期意图 当点击通知的时候执行的意图 
37.         PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
38.         noti.setLatestEventInfo(this, "小芳", "老地方见", contentIntent);
39.         //发送通知
40.         nm.notify(3, noti);
41.     }
42. 
43.     //点击按钮取消同通知
44.    public void click2(View v) {
45.        nm.cancel(3);
46.     }47.    }

上一篇下一篇

猜你喜欢

热点阅读