Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlarmManager did not work after process stops

Tags:

android

I want to set an alarm via AlarmManager class. Normally, It works fine and everything is OK. But if my application process stops or Phone turns off and turns on again, The alarm wont start. Any help? or other service that android provides?

thanks

like image 964
MHM Avatar asked Jan 18 '23 15:01

MHM


1 Answers

Android will erase all intent of alarm manager if you will restart your phone.

You have to create a receiver for start BOOT_COMPLETED and then you will get onReceive() method when your device will start.In this method you can create all alarm again.

You have to declare receiver in manifest

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

And your receiver will be

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyStartupIntentReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(final Context context, Intent intent1) {

        // You can update pending intent here 

    }
}
like image 134
Dharmendra Avatar answered Jan 29 '23 11:01

Dharmendra