首先,发送一个状态栏通知必须用到两个类:NotificationManager、Notification。
NotificationManager:状态栏通知的管理类,负责发通知、清楚通知等。
它必须通过getSystemService()方法来获取:
NotificationManagernm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification:是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
□ 设置一个Notification对象的基本参数:
icon (通知的图标)
title and expanded message (通知的标题和内容)
PendingIntent (点击通知执行页面跳转)
可选的设置:
ticker-text message (状态栏顶部提示消息)
alert sound (提示音)
vibrate setting (振动)
flashing LED setting (灯光)等等
Notificationnotification = new Notification(
R.drawable.ic_launcher,//图标
"Reminder:Meeting starts in 5minutes",//滚动文本
System.currentTimeMillis()); //获取当前的时间
□ 给Notification对象添加属性
//添加震动
notification.defaults |=Notification.DEFAULT_VIBRATE;
//添加声音
notification.defaults |= Notification.DEFAULT_SOUND;
//添加LED灯闪烁
notification.defaults |= Notification.DEFAULT_LIGHTS;
□ Notification附加属性flags:
//重复发出声音,直到用户响应此通知
notification.flags |=Notification.FLAG_INSISTENT;
//在通知栏上点击此通知后自动清除此通知
notification.flags |=Notification.FLAG_AUTO_CANCEL;
//将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |=Notification.FLAG_ONGOING_EVENT;
//表明在点击了通知栏中的"清除通知"后,此通知不清除
notification.flags |=Notification.FLAG_NO_CLEAR;
□ 更新Notification对象
CharSequencecontentTitle = "警告";
CharSequencecontentText = "请删除你手机里面的360软件";
NotificationManagermanager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intentintent = new Intent(MainActivity.this, MyNotification.class);
intent.putExtra("notificationID",NOTIFICATION_ID);
PendingIntentpending = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this,contentTitle, contentText, pending);
manager.notify(NOTIFICATION_ID,notification);
如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
□ 在MyNotification.java类中取消通知
NotificationManagernm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt(“notificationID”));
□ AndroidManifest.xml文件修改
声明activity:
<activityandroid:name=".MyNotification"></activity>
获取震动权限:
<uses-permissionandroid:name="android.permission.VIBRATE"/>
下面看我实现的代码:写代码的一个原则是不要把一个方法一个类写得太冗长,所以我根据上面的划分:
1)首先创建一个类MyNotification.java和一个notification.xml,xml文件中创建一个TextView,写点字上去,太简单了就不说了,MyNotification.java代码如下:
[java] view plain copy print ?
1. public class MyNotification extends Activity {
2. @Override
3. protected void onCreate(Bundle savedInstanceState) {
4. // TODO Auto-generated method stub
5. super.onCreate(savedInstanceState);
6. setContentView(R.layout.activity_notification);
7.
8. NotificationManager manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
9. "notificationID"));
10. }
11. }
2)然后在AndroidManifest.xml文件中,声明MyNotification活动:
[html] view plain copy print ?
1. <activity android:name=".MyNotification"></activity>
3)在MainActivity中的代码如下
(注:import的内容不写出来时因为在eclipse里面按ctrl+shift+字母O就可以自动导入了,没有特殊的我不特意写出来)
[java] view plain copy print ?
1. import .Activity;
2. import .Notification;
3. import .NotificationManager;
4. import .PendingIntent;
5. import android.content.Intent;
6. import android.os.Bundle;
7. import android.view.View;
8.
9. public class MainActivity extends Activity {
10. public final int NOTIFICATION_ID = 1;
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.activity_main);
15. }
16.
17. public void onClickNotification(View v){
18. updataNotification(myNotification());
19. }
20.
21. protected Notification myNotification(){
22. new Notification(
23. R.drawable.ic_launcher,
24. "系统检测到你的手机装了360软件,请尽快删除!",
25. System.currentTimeMillis());
26. //添加震动
27. notification.defaults |= Notification.DEFAULT_VIBRATE;
28. //添加声音
29. notification.defaults |= Notification.DEFAULT_SOUND;
30. return notification;
31. }
32. protected void updataNotification(Notification notification){
33. "警告";
34. "请删除你手机里面的360软件";
35. NotificationManager manager
36. = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
37. //点击该通知后要跳转的Activity
38. new Intent(MainActivity.this, MyNotification.class);
39. "notificationID", NOTIFICATION_ID);
40. this, 0, intent, 0);
41. this, contentTitle, contentText, pending);
42. manager.notify(NOTIFICATION_ID,notification);
43. }
44. }
















