Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager gets killed on app destroy

I have the following problem: I register my alarm manager in onCreate, It gets executed each minute now. However if i kill the app via the Android taskmanager (so the app state is destoyed) the AlarmReceiver stops excecuting. Why?

My Code:

AlarmReceiver

public class AlarmReceiver extends WakefulBroadcastReceiver {

private AlarmManager mAlarm;
private PendingIntent mAlarmIntent;

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, PortalPullService.class);

    startWakefulService(context, service);
}

public void setAlarm(Context context) {
    mAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    mAlarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    int interval = context.getResources().getInteger(R.integer.update_interval_in_mins) * 60 * 1000;

    mAlarm.setInexactRepeating(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + interval,
            interval,
            mAlarmIntent);

    ComponentName reciever = new ComponentName(context, AlarmBootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(reciever,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

public void cancelAlarm(Context context) {
    if(mAlarm != null) {
        mAlarm.cancel(mAlarmIntent);
    }

    ComponentName reciever = new ComponentName(context, AlarmBootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(reciever,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}
}

PullService

public class PortalPullService extends IntentService {

private static final String LOG_TAG = "PortalPullService";

public PortalPullService() {
    super(LOG_TAG);
}

@Override
protected void onHandleIntent(Intent intent) {


    //TODO make request to ** check if new Infos are available, then send notification

    Helper.sendNotification(this, "Test", "Testnotification"); //My test if this works

    AlarmReceiver.completeWakefulIntent(intent);
}

}

AndroidManifest.xml

<receiver android:name=".PortalUpdate.AlarmReceiver" />

<service
        android:name=".PortalUpdate.PortalPullService"
        android:exported="false" />   

The receiver gets registered via new AlarmReceiver().setAlarm(this);

I searched on sveral SO questions, but i can't find an answer... I don't know where is my fault...

Thanks in advance ;)

like image 317
Sebastian Schneider Avatar asked Dec 07 '25 06:12

Sebastian Schneider


1 Answers

Okay it's a problem to install a debug version of your app on Huawei.

Because you can disable Background Services in Huawei, and Huawei doesn't recognize a debug version of your app as a proper app, it destroys all services maybe cause of security reasons ...

Installing it properly over Play Store helps!

like image 170
Ichor de Dionysos Avatar answered Dec 09 '25 19:12

Ichor de Dionysos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!