通知自己打开

时间:2014-03-23 12:52:03

标签: java android notifications

我的应用程序的通知在没有逻辑意义的情况下打开,而不是我编写的代码。我应该每天同时收到通知,但如果我打开应用程序,我会得到相同的通知。这是MainActivity中的代码:

public void setRepeatingAlarm() {
         Intent intent = new Intent(this, MyAlarmService.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
         intent, PendingIntent.FLAG_CANCEL_CURRENT);
         Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 13);
            calendar.set(Calendar.MINUTE, 00);
            calendar.set(Calendar.SECOND, 00);

         am.setRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),24*60*60*1000,pendingIntent);
}

这是MyAlarmService:

public class MyAlarmService extends BroadcastReceiver {

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {
  nm = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  CharSequence from = "Locali Torino";
  CharSequence message = "Visita le serate!";
  Intent action = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    action, 0);



  Notification notif = new Notification(R.drawable.disco,
    "Visita le serate!", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  notif.flags |= Notification.FLAG_AUTO_CANCEL;
  nm.notify(0, notif);  

 }
}

为什么我在打开应用程序时或至少在设定的时间收到通知?

修改

MyAlarmService:

public class MyAlarmService extends BroadcastReceiver {

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {


  nm = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  CharSequence from = "Locali Torino";
  CharSequence message = "Visita le serate!";
  Intent action = new Intent(context, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    action, 0);



  Notification notif = new Notification(R.drawable.disco,
    "Visita le serate!", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  notif.flags |= Notification.FLAG_AUTO_CANCEL;
  nm.notify(0, notif);  
  SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
    SharedPreferences.Editor mEditor = mPref.edit();



    long time = System.currentTimeMillis();
    mEditor.putLong("UPDATE_TIME", time);
    mEditor.commit();
 }And this is MainActivity:

public void setRepeatingAlarm() {
         Intent intent = new Intent(this, MyAlarmService.class);


         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
         intent, PendingIntent.FLAG_CANCEL_CURRENT);

         long startAt;
         long period;

         SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);

         long dif = System.currentTimeMillis() - mPref.getLong("UPDATE_TIME", 0);

         if (dif >= UPDATE_PERIOD) {
           startAt = 0;
           period = UPDATE_PERIOD;
         } else {
           startAt = dif;
           period = dif;
         }


         am.setRepeating(AlarmManager.RTC_WAKEUP, startAt, period,pendingIntent);
}

2 个答案:

答案 0 :(得分:0)

当您注册服务时,服务会运行,因此您需要做的是在注册服务时传递带有intent的布尔值,以防止它在应用程序运行时运行,但之后定期运行。

在您的活动中添加:

intent.putExtra("run", false);

然后在MyAlarmService中:

private boolean run = true;
// ...
action.getBooleanExtra("run", run);
if(run) {
    // notifications code
} else {
    run = true;
}

答案 1 :(得分:0)

实际上它的工作原理与编写代码完全相同。该行为的原因是startAt方法中的setRepeating参数。

 am.setRepeating(AlarmManager.RTC_WAKEUP,
                 calendar.getTimeInMillis(),
                 24*60*60*1000,
                 pendingIntent);

calendar.getTimeInMillis()使用适当的时钟(取决于警报类型),警报首先应以毫秒为单位的时间。

例如:

//send first alarm after 1 second and repeat it every 24 hours
am.setRepeating(AlarmManager.RTC_WAKEUP,
                 1000,
                 24*60*60*1000,
                 pendingIntent);

//send first alarm after 10 second and repeat it every 24 hours
am.setRepeating(AlarmManager.RTC_WAKEUP,
                 10000,
                 24*60*60*1000,
                 pendingIntent);

更新:

您需要保存上次更新时间:

public class MyAlarmService extends BroadcastReceiver {

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {
  //send alarm here
  //...

    SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
    SharedPreferences.Editor mEditor = mPref.edit();
    mEditor.putLong("UPDATE_TIME", time);
    mEditor.commit();
 }
}

然后你可以像这样制作setRepeatingAlarm方法:

public void setRepeatingAlarm() {
         Intent intent = new Intent(this, MyAlarmService.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
         intent, PendingIntent.FLAG_CANCEL_CURRENT);

         long startAt;
         long period;

         SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);

         long dif = System.currentTimeMillis() - mPref.getLong("UPDATE_TIME", 0);

         if (dif >= UPDATE_PERIOD) {
           startAt = 0;
           period = UPDATE_PERIOD;
         } else {
           startAt = dif;
           period = dif;
         }

         am.setRepeating(
               AlarmManager.RTC_WAKEUP,
               startAt,
               period,
               pendingIntent);
}