Imran Hakeem
Imran Hakeem

Reputation: 191

Unable to resume alarm after reboot android

I need to resume an alarm after reboot. I am saving alarm time in SharedPreferences before reboot and after reboot i am setting an alarm with that time but it doesn't fire at all. Here is my code for the alarm in my Helpers class:

public static void scheduleNotification(Notification notification, long delay) {

    Intent notificationIntent = new Intent(AppGlobals.getContext(), AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(AppGlobals.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    AlarmManager alarmManager = (AlarmManager)AppGlobals.getContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    AppGlobals.putAlarmTime(futureInMillis);
    System.out.println("Alarm time before: " + futureInMillis);
}

I am using the above code for scheduling an alarm before reboot in another class and it works fine until a reboot.

Here is my AlarmReceiver class code:

public class AlarmReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);
    Toast.makeText(context, "Alarm!!!", Toast.LENGTH_LONG).show();
}

Code for BootStateListener class:

public class BootStateListenerService extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, AppGlobals.getAlarmTime(), pendingIntent);
        System.out.println("Alarm time after reboot: " + AppGlobals.getAlarmTime());
    }
}

AndroidManifest Code:

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

<application
    android:name=".utils.AppGlobals"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".services.LocationService" />
    <receiver android:name=".services.BootStateListenerService"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

    <receiver android:name=".receivers.AlarmReceiver" />
</application>

I am getting the same time after reboot in Logs which means my bootstatelistener class code is also working but it doesn't fire alarm at all.

Upvotes: 1

Views: 422

Answers (2)

Bhavin Patel
Bhavin Patel

Reputation: 912

Add this in your activity from u call alaram

    ComponentName receiver = new ComponentName(Activity.this, BootStateListenerService .class);
    PackageManager pm = Activity.this.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

Upvotes: 1

Ranjit
Ranjit

Reputation: 5150

Can you try this code inside onReceive()

AlarmManager mgr=
    (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, SomeActivity.class);
PendingIntent pendingIntent =PendingIntent.getService(ctxt, 0, i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
                 AppGlobals.getAlarmTime() + 10000, 10000, pendingIntent);

Upvotes: 0

Related Questions