1. 普通的通知:
/**
* 开启通知
*/
public void startNotification()
{
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "金仔音乐播放器", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "金仔音乐播放器","正在运行", PendingIntent.getActivity(
MyMediaPlayer.this, 0,new Intent(MyMediaPlayer.this,MyMediaPlayer.class), 0));
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
manager.notify(1, notification);
}
2. 添加为正在进行的通知
String service = Context.NOTIFICATION_SERVICE;
NotificationManager manager = (NotificationManager)getSystemService(service);
//实例化Notification
Notification notification = new Notification();
notification.icon = R.drawable.icon;
notification.tickerText = "金仔音乐播放器";
notification.when = System.currentTimeMillis();
//显示在“正在进行中”
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(MyMediaPlayer.this,MyMediaPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//设置启动模式,防止创建新的activity
//获得PendingIntent
PendingIntent pIntent = PendingIntent.getActivity(MyMediaPlayer.this, 0, intent, 0);
//设置事件信息,显示在拉开的里面
notification.setLatestEventInfo(MyMediaPlayer.this, "金仔音乐播放器", "正在运行", pIntent);
//发出通知
manager.notify(1,notification);
3. 设置 FLAG_INSISTENT和FLAG_ONGOING_EVENT标志位让Notification成为持续或正在进行的通知;
正在进行的事件与“普通的”Notification区别在扩展的状态条窗口中
(1)正在进行的:notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
(2)持续的Notification一直重复,直到用户取消:notification.flags = notification.flags | Notification.FLAG_INSISTENT;