notification的使用

notification的使用
1.获取到一个notification服务
2.准备一个notification的信息
3.准备notification的具体信息和点击事件
4.把构建的notification传给notification管理器

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void click(View view){
     //1.获取到一个 notification的服务 
     NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
     //2 .准备一个notification的信息 
     Notification notification = new Notification(R.drawable.notification, "我是notification", System.currentTimeMillis());
     //3. 准备notification的具体信息 和 点击事件
     notification.flags=Notification.FLAG_NO_CLEAR;//设置点击"清除"的操作
    
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_CALL);
     intent.setData(Uri.parse("tel:110"));
     //延期的意图 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
     notification.setLatestEventInfo(this, "我是title", "我是文本", contentIntent);
    
     //4.把我们构建的notification传个notification管理器
     manager.notify(0, notification);
    }
    
     //自定义一个notification
    public void click1(View view){
     //1.获取到一个 notification的服务 
     NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
     // 远程的view
     Notification notification = new Notification();
     RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti);  //要准备一个notification的布局文件
     rv.setTextViewText(R.id.text, "text");   //给远程的控件设置属性.使用到进程间的通信
     rv.setTextViewText(R.id.title, "title");
     //2.创建一个显示的notification

     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_CALL);
     intent.setData(Uri.parse("tel:110"));
     //延期的意图 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
     //3.指定显示的内容
     notification.contentView = rv;
     notification.contentIntent = contentIntent;
     notification.icon = R.drawable.notification;
     notification.tickerText="haha";
     notification.flags = Notification.FLAG_AUTO_CANCEL;
     manager.notify(0, notification);
    
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值