【安卓笔记】notification使用
Notification notification = new Notification(R.drawable.ic_launcher,"您有一条新通知", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;//当用户点击通知时,自动删除通知 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,SecondActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);3)调用setLatestEventInfo方法设置通知的标题,内容,以及点击通知栏所触发的事件等。
notification.setLatestEventInfo(this,"通知标题", "点我跳转到一个activity", contentIntent);
NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, notification);//第一个参数是通知的id全部代码如下:
/** * 显示一个通知,点击通知会跳转到一个Activity * 注:跳转的这个activity必须先在清单文件中注册,否则点击将不会产生效果,而且不抛异常 */ @SuppressWarnings("deprecation") public void sendNotification() { NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher,"您有一条新通知", System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL;//当用户点击通知时,自动删除通知 // 相当于调用了startActivity方法 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,SecondActivity.class),PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this,"通知标题", "点我跳转到一个activity", contentIntent); manager.notify(1, notification); }显示效果:
/** * 普通的通知 *点击通知会跳转到另一个activity */ public void show_normal() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher)//设置图标 .setContentTitle("这是标题")//设置标题 .setContentText("这是内容");//设置内容 /*设置PendingIntent,当用户点击通知跳转到另一个界面,当退出该界面,直接回到HOME*/ Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent);//设置PendingIntent //创建NotificationManager 对象 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = mBuilder.build();//生成Notification对象 notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动关闭通知 mNotificationManager.notify(1,notification); }显示效果:
builder.setProgress(max,progress,boolean inDeterminate);
/** * 带进度条的通知 */ public void show_progress() { final NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.me)); builder.setContentTitle("下载图片"); builder.setContentText("下载进度"); new Thread(new Runnable() { @Override public void run() { for(int i = 0;i<=100;i++) { builder.setProgress(100,i, false); manager.notify(0,builder.build()); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } // 注:setprogress设置为true后进度条进度将会停止,并且进度条不消失 // -- 进度条走完之后更新显示文本-- builder.setContentText("下载完毕").setProgress(0, 0, false); Notification notification = builder.build(); // 下载完成后来一个声音提示 notification.defaults = Notification.DEFAULT_SOUND; manager.notify(0, notification); } }).start(); }显示效果:
“下载完毕”:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。