当前位置: 代码迷 >> Android >> Android-用户揭示Notification
  详细解决方案

Android-用户揭示Notification

热度:85   发布时间:2016-04-28 00:20:35.0
Android-用户提醒Notification
Android-用户提醒Notification
一  Notification
通知:通常与硬件有关的功能都要用到SystemService(跟踪源代码)


操作步骤:
1 定义NotificationManager对象
private NotificationManager mNotificationManager;


2 实例化NotificationManager对象 
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);


3 构造一个Notification对象并且实例化
Notification _notification = new Notification


4 设置事件信息
PendingIntent _pendIntent = PendingIntent.getActivity()


5 开启Notification
mNotificationManager.notify(123, _notification);123为通知的唯一id


PendingIntent的使用:
本身不是intent-设计模式为单件模式(构造模式)(用instance方法只返回一个实例,常用于控制一个实例的不同类型)
PendingIntent _pendIntent = PendingIntent.getActivity( 
MyNotifiationActivity.this,//上下文  
0, //若开启的Activity需要返回值,则这个请求码要有值
new Intent(MyNotifiationActivity.this, MenuTestActivity.class), //intent对象 
0);//flag标志


二 自定义Notification:
1 震动:
默认系统notification.defaults |= Notification.DEFAULT_VIBRATE;
自定义
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate
manifest文件要加上权限<uses-permission android:name="android.permission.VIBRATE"/>


2 声音:
默认系统notification.defaults |= Notification.DEFAULT_SOUND;
自定义notification.sound = Uri.parse("****ringer.mp3");


3 闪光灯:
默认系统notification.defaults |= Notification.DEFAULT_LIGHTS;
自定义
notification.ledARGB = 0xff00ff00;闪光灯颜色
notification.ledOnMS = 300;开启时间
notification.ledOffMS = 1000;关闭时间
notification.flags |= Notification.FLAG_SHOW_LIGHTS;设置flags


4 通知的布局:
为了自定义通知的布局,首先实例化RemoteViews创建一个布局文件,然后把RemoteViews传递给Notification的contentView属性


还需要设置intent
_notification.contentIntent = _pendIntent;


接下来就是自己压入xml的对象实例,之前的默认的_notification.setLatestEventInfo方法不能再使用
_notification.contentView = new RemoteViews(getPackageName(), R.layout.layout_customnotifiation);


注:R.layout.layout_customnotifiation);为自定义的布局文件,不要让你的自定义布局太复杂以确报在不同的配置上测试它。


三 Notification和标志(flags)来为自己的通知做定制
使用notification.flags |= Notification.FLAG_AUTO_CANCEL


FLAG_AUTO_CANCEL标志
使用这个标志可以让在通知被选择之后,通知提示会自动的消失。


FLAG_INSISTENT标志
使用这个标志,可以让提示音循环播放,直到用户响应。


FLAG_ONGOING_EVENT标志
使用这个标记,可以让该通知成为正在运行的应用的通知。 这说明应用还在运行-它的进程还跑在后台,即使是当应用在前台不可见(就像音乐播放和电话通话)。


FLAG_NO_CLEAR标志
使用这个标志,说明通知必须被清除,通过"Clear notifications"按钮。如果你的应用还在运行,那么这个就非常有用了。


number属性
这个属性的值,指出了当前通知的数量。这个数字是显示在状态通知的图标上的。如果你想使用这个属性,那么当第一个通知被创建的时候,它的值要从1开始。而不是零。


iconLevel
这个属性的值,指出了LevelListDrawable的当前水平。你可以通过改变它的值与LevelListDrawable定义的drawable相关联,从而实现通知图标在状态栏上的动画。
  相关解决方案