Question

Hi I am creating an application which will fire the alarm in a particular interval regularly. It works well. But when i switched off my mobile and switched on again alarm not working. Please help to solve the issue.

My Alarm code is:

  AlarmManager alarmMgr = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, ReminderReceiver.class);
            intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
                    alarmId, intent, 0);
            Calendar calendar = Calendar.getInstance();
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
Was it helpful?

Solution 2

Create a BroadCastReceiver and call this put this alarm code in this receiver and add Boot Complete permission to that receiver so when your phone is switched on then that receiver will automatically called. Refer this :

public class Autostart extends BroadcastReceiver 
{
    public void onReceive(Context arg0, Intent arg1) 
    {
        Log.i("Autostart", "**********started************");

        AlarmManager alarmMgr = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, ReminderReceiver.class);
            intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
                    alarmId, intent, 0);
            Calendar calendar = Calendar.getInstance();
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
    }
}

AndroidManifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package_name" android:versionCode="1" android:versionName="1.0"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".Autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>

OTHER TIPS

Add this in your AndroidManifest.xml:

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

and

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

Add this class:

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

public class BootCompletedIntentReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
      if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       Intent pushIntent = new Intent(context, MyService.class);
       context.startService(pushIntent);
      }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top