通知的使用

来源:互联网 发布:苹果mac管理员密码忘记 编辑:程序博客网 时间:2024/05/01 02:34
通知的使用
 


 1,获得通知管理器(通知管理器是用来发送通知)


    NotificationManager nm = (NotificationManager)getSystemService(String name); 
    // name = Context.NOTIFACTION_SERVICE


 2,创建NotificationCompat.Buider对象 (该对象用来创建通知对象)(设计模式 建造者模式)
NotificationCompat.Buider builder = new NotificationCompat.Buider(Context c);




 3,设置通知的相关信息
   builder.setTicker(String text); //设置通知的显示内容
   builder.setDefault(int default); //设置通知的提示铃声,震动,光
   
   设置PendingIntent //设置一个挂起的intent,主要作用:点击通知时需要打开的页面
    
       //第一个参数:编写这个通知的Context对象,
       //第二个参数:是你需要跳转到的页面
     1)Intent intent = new Intent(MainActivity.this,SecondActivity.class);


     2)创建PendingIntent对象
       pendingIntent pi = PendingIntent.getActivity(Context c ,int code , Intent intent,int flag);


     3) 设置pi
      builder.setContentIntent(pi);


  4,获得通知对象
      Notification n = builder.buid();


  5,使用通知管理器发送通知
     
      nm.notify(int id,Notification n); //id-->通知的id ,n-->通知对象
0 0
原创粉丝点击