0

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]

Usman Asif
  • 111
  • 1
  • 1
  • 9

2 Answers2

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
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