Android之Notification:
(1)要发送一个Notification需要一个NotificationManager:
- NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
(2)初始化Notification:
- Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());
- Intent intent = new Intent(this, NotificationActivity.class);
- PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
- notification.setLatestEventInfo(this, "This is content title", "This is content text", pi);
- manager.notify(1, notification);
Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis()); Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); notification.setLatestEventInfo(this, "This is content title", "This is content text", pi); manager.notify(1, notification);
注:
1.Notification的setLatestEventInfo()
第一个参数:上下文;
第二个参数:通知标题内容
第三个参数:通知的正文
第四个参数:PendingIntent,用于相应通知点击跳转。
2.PendingIntent用法:
- Intent intent = new Intent(this, NotificationActivity.class);
- PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
可以根据需求来选择是使用getActivity()方法、getBroadcast()方法、还是getService()方法。
3.NotificationManager的notify():
第一个参数:id,要保证为每个通知所指定的id都是不同的
第二个参数:Notification对象
(3)Notification的取消(消除提示):
(4)Notification高级编程:
- NotificationManager manager = (NotificationManager)
- getSystemService(NOTIFICATION_SERVICE);
- manager.cancel(1);
NotificationManager中cancel中的参数就是上面定义的Notification的id;NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1);
1.指定声音:
2.设定震动:
- Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/ Basic_tone.ogg"));
- notification.sound = soundUri;
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/ Basic_tone.ogg")); notification.sound = soundUri;
- long[] vibrates = {0, 1000, 1000, 1000};
- notification.vibrate = vibrates;
vibrates中参数:long[] vibrates = {0, 1000, 1000, 1000}; notification.vibrate = vibrates;
第一个:手机静止的时长
第二个:手机震动时长
第三个:又表示手机静止的时长
...以此类推
手机震动需要权限:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.notificationtest"
- android:versionCode="1"
- android:versionName="1.0" >
- ……
- <uses-permission android:name="android.permission.VIBRATE" />
- ……
- </manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.notificationtest" android:versionCode="1" android:versionName="1.0" > …… <uses-permission android:name="android.permission.VIBRATE" /> …… </manifest>
3.设置通知Led:
比如实现LED灯以绿色灯光一闪一闪效果:
- notification.ledARGB = Color.GREEN;
- notification.ledOnMS = 1000;
- notification.ledOffMS = 1000;
- notification.flags = Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = Color.GREEN; notification.ledOnMS = 1000; notification.ledOffMS = 1000; notification.flags = Notification.FLAG_SHOW_LIGHTS;
当然可以设置按照默认的方式显示通知:
- notification.defaults = Notification.DEFAULT_ALL;
notification.defaults = Notification.DEFAULT_ALL;