android 拾遗03 --notification
2015-07-12 21:58
363 查看
使用Notification
在android上编程中使用notification分为以下几个步骤构建NotificationManager对象
使用Notification.Builder来构建notification对象
使用NotificationManager的notify方法将通知发送出去
使用时要注意的主要是这几个方面:
1.设置通知显示的标题
2.设置通知显示的时间
3.设置通知显示的内容
4.设置通知显示的大图标
5.设置通知显示的小图标
6.一些默认设置
示例:
public class MainActivity extends Activity { //声明通知的序号,在退出时需要用到 static final int NOTIFICATION_ID=0X123; //声明NotificationManager NotificationManager nm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //使用getsystemService的方法来实例化NotificationManager对象 nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); } public void notificate(View v) { //用来装入PendingIntent的Intent,指示点击Notification跳转的操作 Intent intent =new Intent(this,NextActivity.class); //pendingIntent,使用PendingIntent的getActivity来实例化,第二个和第四个参数是//requestCode和flag,是在StartActivityforResult时用的,一般设置为零就好 // PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0); //使用Notification.Builder来实例化Notification Notification notification=new Notification.Builder(this) //设置点击会自动退出 .setAutoCancel(true) //通知发出时会马上在状态栏显示一个简略的提示,这个指示提示的文字内容 .setTicker("new message") //这个指示状态栏简单提示的文字 .setSmallIcon(R.mipmap.ic_launcher) //下拉在提示栏可以看见详细提示,这个指示提示的标题 .setContentTitle("一条新通知") //这个指示提示的详细文字内容 //.setLargeIcon(Bitmap aBitmap) 这个设置在通知左侧的图片,默认是app图标,必须传Bitmap的参数 .setContentText("你加薪了,加了负五十元哦") //setDefaults可以为通知设置一系列属性为默认值,如声音,震动,灯 .setDefaults(Notification.DEFAULT_SOUND) .setDefaults(Notification.DEFAULT_LIGHTS) .setVibrate(new long[]{0, 50, 100, 150}) //设置通知时间,是UNIX时间格式 .setWhen(System.currentTimeMillis()) //设置点击时触发的事件 .setContentIntent(pi).build(); nm.notify(NOTIFICATION_ID,notification); } public void exit_noti(View v) { //退出这个通知 nm.cancel(NOTIFICATION_ID); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories