When I tried this code, everything is working fine except, service is not getting start after device reboot. I want to start same service automatically. I am testing this example by connecting mobile with USB. what do I need to change ? [http://javatechig.com/android/repeat-alarm-example-in-android]
Asked
Active
Viewed 759 times
0
-
2Try this. [Start AlarmManager if device is rebooted](http://stackoverflow.com/questions/17673746/start-alarmmanager-if-device-is-rebooted) – Quang Doan Dec 23 '15 at 10:43
-
Do you enabled this broadcast receiver "android.intent.action.BOOT_COMPLETED" – Chandrakant Dvivedi Dec 23 '15 at 10:43
-
-
You will find a full example of Alarms surviving reboots in the official docs. – Phantômaxx Dec 23 '15 at 11:11
-
google `android scheduling repeating alarms` – Phantômaxx Dec 23 '15 at 12:43
2 Answers
1
try like this
<!-- for reboot event to reset alarms -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
then
<receiver
android:name="com.yourapp.receiver.RestartAppReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
next you have to create the BroadcastReceiver class
public class RestartAppReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "RestartAppReceiver";
public RestartAppReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
Log.i(LOG_TAG, "Start resetting alarms after reboot");
//restart what you need
Log.i(LOG_TAG, "Finish resetting alarms after reboot");
break;
default:
break;
}
}
}
}

Jithu P.S
- 1,843
- 1
- 19
- 32
-
-
@UsmanAsif No need to make a call.define the manifest like this,then it will works when the reboot event happens – Jithu P.S Dec 23 '15 at 12:35
0
You have to create a broadcast receiver that will listen for boot complete event and when that event is received start your service again
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
create a class like this and add your code in onReceive method
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}

Vivek Mishra
- 5,669
- 9
- 46
- 84