[英]Notification is shown everytime I close the app
I created a notification which is shown at specific time, but the problem is that everytime I close the app (after the prefixed time) the noification is shown. 我创建了在特定时间显示的通知,但是问题是每次关闭应用程序(在前缀时间之后)都会显示提名。 How can I fix this issue? 如何解决此问题?
This is my code: Home.class (is a fragment) 这是我的代码: Home.class (是一个片段)
Calendar calend = Calendar.getInstance();
calend.setTimeInMillis(System.currentTimeMillis());
calend.set(Calendar.HOUR_OF_DAY, 9);
calend.set(Calendar.MINUTE, 27);
calend.set(Calendar.SECOND, 0);
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calend.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
MyReceiver.class MyReceiver.class
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService.class MyAlarmService.class
public class MyAlarmService extends Service
{
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
I have an answer for this. 我对此有一个答案。
don't use service for the same. 不要将服务用于同一目的。 you just need to write the below code in your MyReceiver class and it will not give you notification when you kill the app from task manager. 您只需要在MyReceiver类中编写以下代码,当您从任务管理器中删除该应用程序时,它不会给您通知。
public class AlarmReceiver extends BroadcastReceiver {
private NotificationManager mManager;
private static final int MY_NOTIFICATION_ID = 1;
private static final String TAG = "AlarmNotificationReceiver";
// Notification Text Elements
private final CharSequence tickerText = "its ok?";
private final CharSequence contentTitle = " Reminder1";
private final CharSequence contentText = "reminder content";
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
@Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
mNotificationIntent = new Intent(context, MyView.class);
// The PendingIntent that wraps the underlying Intent
mContentIntent = PendingIntent.getActivity(context, 0,mNotificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
// Build the Notification
Notification.Builder notificationBuilder = new Notification.Builder(
context).setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true).setContentTitle(contentTitle)
.setContentText(contentText).setContentIntent(mContentIntent);
// Get the NotificationManager
NotificationManager mNotificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Pass the Notification to the NotificationManager:
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
}
}
Has you can read in this post : Will AlarmManager work if my application is not running? 您可以在这篇文章中阅读: 如果我的应用程序未运行,AlarmManager是否可以工作? The AlarmManager keep you Alarm until the device is reboot OR if you kill the app. AlarmManager会向您发出警报,直到设备重新启动或您杀死该应用程序为止。 Remove it from the recent list will kill the app. 从最近的列表中删除它会杀死该应用程序。
This did not explain why the notification is shown but I guess the system execute your request during the killing. 这并不能解释为什么显示通知,但是我想系统会在杀死期间执行您的请求。 It should just remove it but maybe (no proof here) it prefere to send the broadcast insteed 它应该只是将其删除,但也许(此处没有证据)它更喜欢发送广播指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.