□ 首先,发送一个状态栏通知必须用到两个类: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代码如下:
public class MyNotification extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
NotificationManager manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getExtras().getInt("notificationID"));
}
}
2)然后在AndroidManifest.xml文件中,声明MyNotification活动:
<activity android:name=".MyNotification"></activity>
3)在MainActivity中的代码如下
(注:import的内容不写出来时因为在eclipse里面按ctrl+shift+字母O就可以自动导入了,没有特殊的我不特意写出来)
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
public final int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickNotification(View v){
updataNotification(myNotification());
}
protected Notification myNotification(){
Notification notification = new Notification(
R.drawable.ic_launcher,
"系统检测到你的手机装了360软件,请尽快删除!",
System.currentTimeMillis());
//添加震动
notification.defaults |= Notification.DEFAULT_VIBRATE;
//添加声音
notification.defaults |= Notification.DEFAULT_SOUND;
return notification;
}
protected void updataNotification(Notification notification){
CharSequence contentTitle = "警告";
CharSequence contentText = "请删除你手机里面的360软件";
NotificationManager manager
= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//点击该通知后要跳转的Activity
Intent intent = new Intent(MainActivity.this, MyNotification.class);
intent.putExtra("notificationID", NOTIFICATION_ID);
PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, pending);
manager.notify(NOTIFICATION_ID,notification);
}
}
当然activity_main.xml文件中创建了一个Button,太简单了就不说了。
好累啊,今天一整天都在写博客,把一段时间内的复习总结。