android笔记16-Notification

来源:互联网 发布:linux c程序编译 编辑:程序博客网 时间:2024/06/08 08:05
Notification:状态栏通知;
通知栏的内容:
 图标,标题,内容,时间,点击后的响应;

Notification.Builder的方法:
  setSmallIcon(R.drawable...):设置图标;  setTicker(string):设置手机状态栏的提示;
  setWhen(System.currentTimeMillis()):时间;
  setContentTitle(String):设置标题;
  setContentText(String):设置通知内容;
  setContenIntent(PendingIntent):设置点击响应;
  setDefaults(Notification.DEFAULT_SOUND):设置提示声音,Notification.DEFAULT_LIGHTS:设置指示灯,Notification.DEFAULT_VIBRATE:设置震动效果;Notification.DEFAULT_ALL:包括以上三种;


注意设置指示灯或震动效果时都需要权限:
 在Manifest的permission中:android.permission.FLASHLIGHT--指示灯;
android.permission.VIBRATE--震动;


实现通知栏需要两个类:
 通知管理类,通知类;

设置通知参数步骤:
 第一步:创建Builder对象(是notification的builder)并new出Notification.Builder(this),通过调用builder的方法来设置,setSmallIcon(R.drawable...),setTicker...;
 第二步(点击后的响应):创建PendingIntent对象并赋值为PendingIntent.getActivity(context,requestCode,intent,flags):
  context:this;
  requestCode:请求码,0;
  intent:创建Intent对象,在new中根据需求选择构造的类.class;
  flags--0;
 第三步:创建Notification对象,并将builder.build()赋值//4.1即以上,要用builder.build()方法,以下要用builder.getNotification()方法;
 第四步:创建NotificationManager对象,因为是系统的常用服务,赋值为getSystemService(Context.NOTIFICATION_SERVICE),需强制转化;调用成员函数notify(id,notification)来加载Notification,id是一个int值,表示notification的id,自行赋值即可;


取消通知:
 调用manager.cancle(id);id为diaoyongnotify函数时,填入的id;
0 0
原创粉丝点击