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>