【问题标题】:Notification is shown everytime I close the app每次我关闭应用程序时都会显示通知
【发布时间】:2015-06-04 13:48:09
【问题描述】:

我创建了一个在特定时间显示的通知,但问题是每次我关闭应用程序(在前缀时间之后)都会显示通知。我该如何解决这个问题?

这是我的代码: 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

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

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();
    }

}

【问题讨论】:

  • 请提供您放置上述代码的位置。
  • 你实际上在alarmManager.setRepeating 上创建了一个Alarm,它会重复它。当警报在Service 上运行时关闭应用程序时取消它
  • “我该如何解决这个问题?”:离开应用时取消闹钟。
  • 如果我取消闹钟,通知将不会显示?例如,如果我将闹钟设置在上午 10 点,并且在我离开应用程序时取消闹钟,如果用户没有打开应用程序通知将在上午 10 点显示后的第二天?
  • @Slaiv206 :抱歉,您没有很好地解释您的问题。请进一步解释您的应用程序的功能、通知的用途、您期望发生的事情以及实际发生的事情。你所做的只是说你的通知在不应该显示的时候显示,并发布了一些标准代码来设置一个不告诉我们任何东西的警报。

标签: android android-notifications android-alarms android-calendar


【解决方案1】:

我有一个答案。

不要使用相同的服务。您只需在 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());

        }

    } 

【讨论】:

  • 这实际上似乎对我有用。但是任何人都可以给出相同的解释吗?
【解决方案2】:

你能读到这篇文章吗: Will AlarmManager work if my application is not running? AlarmManager 会一直提醒您,直到设备重新启动或您终止应用程序。从最近列表中删除它会杀死该应用程序。

这并没有解释为什么显示通知,但我猜系统在杀戮期间执行了您的请求。它应该只是删除它,但也许(这里没有证据)它更喜欢发送广播

【讨论】:

  • 好的,但我会尝试在设置通知的时间前一分钟终止该应用程序,并且在显示通知的正确时间时终止该应用程序,即使我终止了该应用程序。而且我看到在我关闭应用程序的确切时刻和一段时间后杀死应用程序后也会显示通知。
  • 你能用你完成的所有测试来编辑你的问题吗?因为现在还不清楚。
最近更新 更多