Search code examples
androidbroadcastreceiveralarmmanager

Alarm manager is not triggering if app is killed


I am trying to create a scheduling application. I am using Alarm manager. It is working fine when app is alive. If I removed the app from recent applications Alarm is not triggering. Please let me any idea to resolve my issue. Herewith I attached my code.

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(SettingsPage.this, AlarmManagerForSettings.class);
i.putExtra("requestCode", "100");
i.putExtra("AlarmTag",id);
PendingIntent pi = PendingIntent.getBroadcast(SettingsPage.this, (int)id, i, 0);
am.cancel(pi); // cancel any existing alarms RTC_WAKEUP
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY, pi);
ComponentName receiver = new ComponentName(SettingsPage.this, AlarmManagerForSettings.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);

Broadcast Receiver:-

public class AlarmManagerForSettings extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String requestCode = "";
        String alarmTag = "";
        if (bundle != null) {
            requestCode = bundle.get("requestCode").toString();
            alarmTag = bundle.get("AlarmTag").toString();
        }
//Rest of code I wrote here
   }
}

Manifest:-

<receiver
  android:name=".AlarmManagerForSettings"
  android:enabled="true"
  android:exported="true">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"></action>
  </intent-filter>
 </receiver>

Solution

  • Some of the mobile Broadcast Receiver is not running..if Broadcast is not running follow below steps: In Xiaomi devices, you just have to add your app to Autostart list, to do so, follow these simple steps given below:

    1.Open Security app on your phone.

    2.Tap on Permissions, it'll show you two options: Autostart and Permissions

    3.Tap on Autostart, it'll show you list of apps with on or off toggle buttons.

    4.Turn on toggle of your app, you're done!

    How to get MIUI Security app auto start permission programmatically?