当我的 MainActivity 为 onPause() 时,我必须显示通知,所以我在 MainActivity 上编写下面的代码,所以 onPAuse() 我的意思是 Activity 由于外部操作(未接来电、屏幕锁定)而中断,但我有一个问题,即使当我移动到另一个活动时,也会显示通知,有人可以帮助我吗?
private void createNotify(){
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Radio Jawhara en cours...", System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, FragmentsSliderActivity.class), 0);
String titreNotification = "Radio Jawhara en cours...";
String texteNotification = "Retour à la radio...";
notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent);
notification.vibrate = new long[] {0,200,100,200,100,200};
notificationManager.notify(ID_NOTIFICATION, notification);
}
private void cancelNotify(){
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(ID_NOTIFICATION);
}
@Override
protected void onResume() {
super.onResume();
cancelNotify();
}
@Override
protected void onPause() {
super.onPause();
createNotify();
}