简体   繁体   中英

AlarmManager going off everytime i open the app

I am running this code to set a Alarm Manager in my MainActivity on create method

public void notificationCheck() {

    calendar.set(Calendar.HOUR_OF_DAY, Preferences.getMorningHour(getApplicationContext()));
    calendar.set(Calendar.MINUTE, Preferences.getMorningMinute(getApplicationContext()));
    calendar.set(Calendar.SECOND, 0);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), 0);

    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.setAndAllowWhileIdle(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    } else {
        am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

So this should set the alarm daily at 10:00 AM, The alarm is working fine until it's fired once. Once the alarm is done at 10:00 AM it keeps going off each time i open the app.

Can some please explain if i need to make any code changes?

EDIT :

I am using sharedPreferences to set the time of the Calendar Instance

Preferences:

public static int getMorningHour(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(MORNING_HOUR, 9);
}

public static void setMorningHour(Context context, Integer morningHour) {
    PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(MORNING_HOUR, morningHour).apply();
}

public static int getMorningMinute(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(MORNING_MINUTE, 0);
}

public static void setMorningMinute(Context context, Integer morningMinute) {
    PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(MORNING_MINUTE, morningMinute).apply();
}

... and I set the Preferences in my app Settings using TimePicker Dialog

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {

    Preferences.setMorningHour(getApplicationContext(), hourOfDay);
    Preferences.setMorningMinute(getApplicationContext(), minute);

}

when start Activity , you need to check alarm already live or not. if live dnt disturb that. see below code.

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

boolean isWorking = (PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null);
if (isWorking) {Log.d("alarm", "is working");} else {Log.d("alarm", "is not working");}

if(!isWorking) {
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,    PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    int timeNotif = 5 * 60 * 1000;//time in ms, 7*24*60*60*1000 for 1 week
    Log.d("Notif", "Notification every (ms): " + timeNotif);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timeNotif, pendingIntent);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM