简体   繁体   中英

Why Alarm Manager does not working from Intent Service Class?

I am working on task to trigger alarm at specific time my alarms working fine before reboot but after device reboot broadcast receiver does not trigger my service class where i reset the alarms from shared preference. I read some post on facebook they said alarm manager doesn't work properly use Work manager instead. Which one will be good approach for my task. Hope I will get good solution.

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.edu.pk.gulehri.meraallah">

<!-- Permissions that App Requires -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MeraAllah">

    <!-- Activities -->
    <activity android:name=".ui.SilentPhoneActivity" />
   
    <!-- Splash Screen As Launcher Activity -->
    <activity
        android:name=".ui.SplashScreen"
        android:noHistory="true"
        android:theme="@style/SplashScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Registering Broadcast MyBroadCastReceiver -->
    <receiver
        android:name="com.edu.pk.gulehri.meraallah.receivers.MyBroadCastReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.edu.pk.gulehri.meraallah.services.MyService"
        android:enabled="true" />
</application>

MyBroadcastReceiver.java

public class MyBroadCastReceiver extends BroadcastReceiver {


private static final String BOOT_COMPLETED =
        "android.intent.action.BOOT_COMPLETED";
private static final String QUICKBOOT_POWERON =
        "android.intent.action.QUICKBOOT_POWERON";

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

    String action = intent.getAction();
    if (action.equals(BOOT_COMPLETED) ||
            action.equals(QUICKBOOT_POWERON)) {
        Intent service = new Intent(context, MyService.class);
        context.startService(service);
        //context.stopService(service);

        Toast.makeText(context, "BOOT COMPLETED", LENGTH_LONG).show();
        Log.i(Constants.TAG, "onReceive: Device Reboot");
    } else {
        setRinger(context);
    }}

private void setRinger(@NonNull Context context) {
    AudioManager audio = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if ((audio.getRingerMode() == RINGER_MODE_NORMAL) ||
            (audio.getRingerMode() == RINGER_MODE_VIBRATE)) {
        audio.setRingerMode(RINGER_MODE_SILENT);

    } else if (audio.getRingerMode() == RINGER_MODE_SILENT) {
        audio.setRingerMode(RINGER_MODE_NORMAL);


       }
    }
}

MyService.java

public class MyService extends IntentService {


public MyService() {
    super("MyService");
}

public void getValues(@NonNull Context context) {
    SharedPreferences sp = context.getSharedPreferences(ALARM_VALUES, MODE_PRIVATE);
    Gson gson = new Gson();

    //Flags For Alarms
    FAJAR_FLAG = sp.getBoolean(FAJAR_FLAG_VALUE, false);
    ZUHUR_FLAG = sp.getBoolean(ZUHUR_FLAG_VALUE, false);
    ASR_FLAG = sp.getBoolean(ASR_FLAG_VALUE, false);
    MAGHRIB_FLAG = sp.getBoolean(MAGHRIB_FLAG_VALUE, false);
    ISHA_FLAG = sp.getBoolean(ISHA_FLAG_VALUE, false);
    JUMMAH_FLAG = sp.getBoolean(JUMMAH_FLAG_VALUE, false);


    CALENDER_FOR_FAJAR = gson.fromJson(sp.getString(CALENDER_FAJAR, ""), Calendar.class);
    CALENDER_FOR_ZUHUR = gson.fromJson(sp.getString(CALENDER_ZUHUR, ""), Calendar.class);
    CALENDER_FOR_ASR = gson.fromJson(sp.getString(CALENDER_ASR, ""), Calendar.class);
    CALENDER_FOR_MAGHRIB = gson.fromJson(sp.getString(CALENDER_MAGHRIB, ""), Calendar.class);
    CALENDER_FOR_ISHA = gson.fromJson(sp.getString(CALENDER_ISHA, ""), Calendar.class);
    CALENDER_FOR_JUMMAH = gson.fromJson(sp.getString(CALENDER_JUMMAH, ""), Calendar.class);
}


public void setAlarms(Context context) {

    Intent i = new Intent(context, MyBroadCastReceiver.class);

    if (FAJAR_FLAG) {

        AlarmManager fajarAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        PendingIntent PIFS = PendingIntent.getBroadcast(context, 1, i, FLAG_UPDATE_CURRENT);
        PendingIntent PIFN = PendingIntent.getBroadcast(context, 11, i, FLAG_UPDATE_CURRENT);

        if (CALENDER_FOR_FAJAR.before(Calendar.getInstance())) {
            CALENDER_FOR_FAJAR.add(DATE, 1);
        }
        SharedPreferences sharedPreferences = context.getSharedPreferences(ALARM_VALUES, MODE_PRIVATE);
        fajarAlarm.setRepeating(RTC_WAKEUP, sharedPreferences.getLong("C1", 0), DELAY, PIFS);
        fajarAlarm.setRepeating(RTC_WAKEUP, sharedPreferences.getLong("C1", 0) + 180000, DELAY, PIFN);


    } else if (ZUHUR_FLAG) {

        AlarmManager zuhurAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        PendingIntent PIZS = PendingIntent.getBroadcast(context, ZUHUR_RC_SILENT, i, 0);
        PendingIntent PIZN = PendingIntent.getBroadcast(context, ZUHUR_RC_NORMAL, i, 0);

        if (CALENDER_FOR_ZUHUR.before(Calendar.getInstance())) {
            CALENDER_FOR_ZUHUR.add(DATE, 1);
        }
        zuhurAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ZUHUR.getTimeInMillis(), DELAY, PIZS);
        zuhurAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ZUHUR.getTimeInMillis() + EXTRA_TIME_FOR_NORMAL, DELAY, PIZN);


    } else if (ASR_FLAG) {
        AlarmManager asrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        PendingIntent PIAS = PendingIntent.getBroadcast(context, ASR_RC_SILENT, i, 0);
        PendingIntent PIAN = PendingIntent.getBroadcast(context, ASR_RC_NORMAL, i, 0);

        if (CALENDER_FOR_ASR.before(Calendar.getInstance())) {
            CALENDER_FOR_ASR.add(DATE, 1);
        }
        asrAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ASR.getTimeInMillis(), DELAY, PIAS);
        asrAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ASR.getTimeInMillis() + EXTRA_TIME_FOR_NORMAL, DELAY, PIAN);


    } else if (MAGHRIB_FLAG) {

        AlarmManager maghribAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        PendingIntent PIMS = PendingIntent.getBroadcast(context, MAGHRIB_RC_SILENT, i, 0);
        PendingIntent PIMN = PendingIntent.getBroadcast(context, MAGHRIB_RC_NORMAL, i, 0);

        if (CALENDER_FOR_MAGHRIB.before(Calendar.getInstance())) {
            CALENDER_FOR_MAGHRIB.add(DATE, 1);
        }
        maghribAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_MAGHRIB.getTimeInMillis(), DELAY, PIMS);
        maghribAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_MAGHRIB.getTimeInMillis() + EXTRA_TIME_FOR_NORMAL, DELAY, PIMN);

    } else if (ISHA_FLAG) {

        AlarmManager ishaAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);


        PendingIntent PIIS = PendingIntent.getBroadcast(context, ISHA_RC_SILENT, i, 0);
        PendingIntent PIIN = PendingIntent.getBroadcast(context, ISHA_RC_NORMAL, i, 0);

        if (CALENDER_FOR_ISHA.before(Calendar.getInstance())) {
            CALENDER_FOR_ISHA.add(DATE, 1);
        }
        ishaAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ISHA.getTimeInMillis(), DELAY, PIIS);
        ishaAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_ISHA.getTimeInMillis() + EXTRA_TIME_FOR_NORMAL, DELAY, PIIN);

    } else if (JUMMAH_FLAG) {

        AlarmManager jummahAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        PendingIntent PIJS = PendingIntent.getBroadcast(context, JUMMAH_RC_SILENT, i, 0);
        PendingIntent PIJN = PendingIntent.getBroadcast(context, JUMMAH_RC_NORMAL, i, 0);

        if (CALENDER_FOR_JUMMAH.before(Calendar.getInstance())) {
            CALENDER_FOR_JUMMAH.add(DATE, 1);
        }
        jummahAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_JUMMAH.getTimeInMillis(), DELAY, PIJS);
        jummahAlarm.setRepeating(RTC_WAKEUP, CALENDER_FOR_JUMMAH.getTimeInMillis() + EXTRA_TIME_FOR_NORMAL, DELAY, PIJN);

    }

}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    getValues(this);
    setAlarms(this);
    Intent i = new Intent(this, MyService.class);
    this.stopService(i);
}

}

You cannot start services from API 26 unless they are started when app is running from having used its icon to launch it (which is not the case for something that executes on boot).

You need to transform all your services to JobIntentServices, if they are started outside there. (Maybe there's also other possible solutions, but that's the one I know).

Here some reference about JobIntentService .

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