AlarmManager in android runs every time i open the app

a 夏天 提交于 2020-01-17 17:01:53

问题


I have a code for creating an alarmManager main activity

Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 41);
c.set(Calendar.SECOND, 0);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC,c.getTimeInMillis(), pendingIntent.getBroadcast(this, 0, intentAlarm, pendingIntent.FLAG_ONE_SHOT));

In the AlarmReceive class I have:

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
    intent = new Intent(context, firstservice.class);
    context.startService(intent);
}

In service firstservice I did some operation. The problem is that the alarm is calling the broadcast receiver on time but after that when I open the app it also call the broadcast receiver and this mean that the service is worked again.


回答1:


I also had this problem. After copple of hours, I could point out the problem with this solution. In brief I have used intent extras to notify the alarm time.

in the manifest.xml, register the receiver.

 <receiver android:name=".receivers.DayAlarmReceiver"/>

This is how I set the alarm manager

 public void setDayAlarm(Context context) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 4);

    AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(context, DayAlarmReceiver.class);
    alarmIntent.putExtra("HOUR_OF_DAY", 4);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5555, alarmIntent, 0);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
    Log.d("Set the day alarm");

}

This is the OnReceive() method of my DayAlarmReceiver class

@Override
public void onReceive(Context context, Intent intent) {
    int setHour = intent.getIntExtra("HOUR_OF_DAY", -1);
    Calendar rightNow = Calendar.getInstance();
    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);

    if(currentHour == setHour){
        Log.d("This is perfect, Trigger the alarm");
        showNotification(context, msg);
    }else{
       Log.d("This is perfect, Trigger the alarm");

    }

Happy coding :)




回答2:


Try to add an extra to your intent and check it to distinguish your scheduled alarms from any other broadcast your receiver is served

...
c.set(Calendar.SECOND, 0);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
intentAlarm.putExtra("myalarm",Boolean.TRUE)
AlarmManager alarmManager = (AlarmManager) ...



public void onReceive(Context context, Intent intent) {
     if (null != intent){
             Bundle extras = intent.getExtras();

             if(extras != null && extras.getBoolean("myalarm", Boolean.TRUE)){
                  //Do your actions  ...  
             }
      }
}


来源:https://stackoverflow.com/questions/35363633/alarmmanager-in-android-runs-every-time-i-open-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!