Alarmmanager not working after phone reboot

6.9k views Asked by At

I had created alarmmanager on button click. but it not working after phone reboot. my AlarmbroadcastReceiver not call on phone reboot. it work when phone lock , application killed but not work after phone reboot i had created one progressbar which start on button click and stop after alarm broadcast fired but it not stop when phone reboot. i had added my button click event and broadcast receiver class

Button click event

b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
pb1.setVisibility(View.VISIBLE);
progress_edit.putBoolean("progress_one", true);
progress_edit.apply();
                    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class);
                    intnt.setAction("com.ex.Alarm");
                    PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
                    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
                                Log.d("Broadcast ","Fired");
                }
            });

BroadcastReceiver Class

    @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("inside","broadcast receive");
            if(intent.getAction().equalsIgnoreCase("com.ex.Alarm"))
            {
enterSys_progress_edit.putBoolean("progress_one", false);
                enterSys_progress_edit.apply();
                Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show();
            }

        }

My Manifest file

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.krutarth.alarm">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".AlarmbroadcastReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>
2

There are 2 answers

10
Ak9637 On BEST ANSWER

all alarms are reset when phone restart ,So create a callback on reboot like this

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       ////// reset your alrarms here 
    }

}

}

ALSO ADD THIS TO YOUR MANIFEST TO REGISTER THE BROADCAST

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

Create a method say,

setMyalarm(){
     // here set the alarm as u need
}

now call this method setMyAlarm ,whenever and wherever u need to set that particular alarm,whether its on a button click or whether it on a reboot reciever

1
tej shah On

prefect ans by Ak9637 but didn't forgot to add permisttion of

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>