简体   繁体   English

每次关闭应用程序时都会显示通知

[英]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.

相关问题 每次我打开和关闭我的应用程序时都会出现异常 - Exceptions coming everytime I open and close my app 当我关闭我的应用程序时,通知不起作用 - Notification is not working when i close my app 我关闭应用程序后通知无法正常工作 - Notification not working after I close app 我关闭应用程序时未显示通知 - Notification not showing up when I close the app 关闭应用程式后,GCM通知会导致应用程式崩溃 - GCM Notification crashes app after I close the app Ionic 2应用程序每次收到推送通知时都会停止 - Ionic 2 app stops everytime it received a push notification 每次单击主页按钮后,如何获取android后退按钮以关闭应用程序? - How can i get the android back button to close the app everytime the home button is clicked? 如何创建在单击后关闭应用程序的通知? - How can I create notification which will close the app after clicking? 当我关闭应用程序...并打开通知...我在TestActicty上点击它关闭应用程序并且不要回到主要的Actitvty - When I close The Application … And Open The Notification … I Click On Back on TestActicty it close the app and dont back to main Actitvty 通知栏单击以关闭应用程序 - Notification bar click to close app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM