android中的notification
2016-03-31 14:25
489 查看
在手机使用中我们常常会接到应用程序向用户发送的通知,在android中 我们用notification这个装载信息发送给用户, 一般叫它通知, 而管理通知时(比如发送和取消)要用NotificationManager这个类.
用户在接到通知时,状态栏上会有通知的小图标,同时有接到的通知音效等,向下拉时展示具体的内容.
接下来介绍一下通知的用法和两种视觉效果.
在介绍之前做一些准备工作:
1.notification的构建方法
在API4之前 notification的构建方法推荐用Notification.Builder
在notification中必须要设置的属性:
// 1.contentTitle 通知栏消息标题
// 2.contentText 通知栏消息内容
// 3.smallIcon 小的icon
在这里我们可以看出,除了需要要设置的属性, 还有几个属性也非常重要, 比如setWhen , setTicker ,setAutoCancel等等,这里有一个setcontentIntent,稍后再说.
在设置完notification后,manager调用notify()的方法将通知发送出去,其中的两个参数一个是notification的ID 做为消息的标识,是开发者设置的,另一个就是刚刚构建好的notification对象,至此 我们就能完整的发送一个通知了.
如果想取消通知 怎么办呢?
在API4之后,notification的构建方法推荐使用NotificationCompat.Builder
以上两种构建方法 , 都是标准视图,在android中是通用的,但在android4.1之后,compat可以设置notification的style,有三种style分别是:NotificationCompat.BigPictureStyle NotificationCompat.BigTextStyle NotificationCompat.InboxStyle
在API中有详细的用法介绍:
其他两种就不介绍了,API里写的很明白
在消息过程中,还有一个重要的类用来实现页面跳转,PendingIntent 是未决定的intent ,
pendingIntent在跳转时,有三种获取方式
1、getActivity(Context, int, Intent, int),跳转到一个activity组件
2、getBroadcast(Context, int, Intent, int),打开一个广播组件
3、getService(Context, int, Intent, int),打开一个服务组件
具体的用法:
通过setContentIntent()方法,可以设置点击通知后跳转的方式
用户在接到通知时,状态栏上会有通知的小图标,同时有接到的通知音效等,向下拉时展示具体的内容.
接下来介绍一下通知的用法和两种视觉效果.
在介绍之前做一些准备工作:
private Button sendButton, cancelButton,bigPictureButton,inboxStyleButton; private NotificationManager manager; private int NOTIFICATION_ID = 2002; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); sendButton = (Button) findViewById(R.id.send_button); cancelButton = (Button) findViewById(R.id.cancel_button); bigPictureButton = (Button) findViewById(R.id.big_pic); inboxStyleButton = (Button) findViewById(R.id.inbox); sendButton.setOnClickListener(this); cancelButton.setOnClickListener(this); bigPictureButton.setOnClickListener(this); inboxStyleButton.setOnClickListener(this); }
1.notification的构建方法
在API4之前 notification的构建方法推荐用Notification.Builder
public void onClick(View v) { switch (v.getId()) { case R.id.send_button: Bitmap largeIcon0 = BitmapFactory.decodeResource(getResources(), R.mipmap.aa); //3.0之前的版本 Notification notification = new Notification.Builder(this) .setContentTitle("memmememe") .setContentText("lallalallal") .setLargeIcon(largeIcon0) .setSmallIcon(R.mipmap.ic_launcher) //设置默认的声音等 .setDefaults(Notification.DEFAULT_ALL) //设置时间 .setWhen(System.currentTimeMillis()) //3.0前可以设置未读消息有多少条 .setNumber(13) //点击后自己消失 .setAutoCancel(true).build(); manager.notify(NOTIFICATION_ID, notificationCompat.build()); break;
在notification中必须要设置的属性:
// 1.contentTitle 通知栏消息标题
// 2.contentText 通知栏消息内容
// 3.smallIcon 小的icon
在这里我们可以看出,除了需要要设置的属性, 还有几个属性也非常重要, 比如setWhen , setTicker ,setAutoCancel等等,这里有一个setcontentIntent,稍后再说.
在设置完notification后,manager调用notify()的方法将通知发送出去,其中的两个参数一个是notification的ID 做为消息的标识,是开发者设置的,另一个就是刚刚构建好的notification对象,至此 我们就能完整的发送一个通知了.
如果想取消通知 怎么办呢?
case R.id.cancel_button: manager.cancel(NOTIFICATION_ID); break;
在API4之后,notification的构建方法推荐使用NotificationCompat.Builder
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.aa); NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(this); notificationCompat.setContentTitle("mememem") .setContentText("lallala") .setLargeIcon(largeIcon) .setSmallIcon(R.mipmap.ic_launcher) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setAutoCancel(true); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(NOTIFICATION_ID, notificationCompat.build());
以上两种构建方法 , 都是标准视图,在android中是通用的,但在android4.1之后,compat可以设置notification的style,有三种style分别是:NotificationCompat.BigPictureStyle NotificationCompat.BigTextStyle NotificationCompat.InboxStyle
在API中有详细的用法介绍:
其他两种就不介绍了,API里写的很明白
在消息过程中,还有一个重要的类用来实现页面跳转,PendingIntent 是未决定的intent ,
Intent intent = new Intent(MainActivity.this, SecondActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent在跳转时,有三种获取方式
1、getActivity(Context, int, Intent, int),跳转到一个activity组件
2、getBroadcast(Context, int, Intent, int),打开一个广播组件
3、getService(Context, int, Intent, int),打开一个服务组件
具体的用法:
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(this); notificationCompat.setContentTitle("mememem") .setContentText("lallala") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pendingIntent) manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(NOTIFICATION_ID, notificationCompat.build());
通过setContentIntent()方法,可以设置点击通知后跳转的方式
相关文章推荐
- 解析二进制文件AndroidManifest.xml
- 【转】Android 当打开“开发者模式”中的“不保留活动”后,程序应当怎么保持正常运行
- Android 实现自定义宽高比的ImageView
- Android安卓破解之逆向分析SO常用的IDA分析技巧
- Android新手入门2016(17)--新手总结
- android studio添加android.support.v4.widget.ViewDragHelper包
- Android UI控件
- android layout布局属性、标签属性总结大全
- 关于Android布局优化(一)
- Android studio 不能预览布局文件
- Android屏幕适配全攻略(最权威的官方适配指导)
- android逆向总结---强哥的书
- Android开发之WebView和JS交互详解
- android getwidth 为0解决方案
- 浅谈Activity之启动方式(5种启动方式和隐式启动)
- Android--短信拦截及IP拨号
- Android Studio优化之启用Shift+Ctrl+O导入所有的包
- android mediarecorder自制录相机花屏问题
- Android动画 Animation
- Android利用系统Action裁剪图片大小