Search code examples
androidandroid-manifestmanifestandroid-alarms

Running service at fixed time


I am following this

But I don't want to restart the phone each time to update the REPEAT_TIME. And I have something weird on manifest

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

and

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

I don't want anything related to BOOT on my app. What can I do? EDIT: And when I completely uninstall the app and try to run it. The service won't start. I need to restart the phone and run it again. Why so? I don't want to restart the phone


Solution

  • Adding

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

    to your manifest will ensure that the MyScheduleReceiver is called when the android device reboots.

    As to why you might need this :

    If you set up Alarms using the AlarmManager to schedule something, android will kill your alarm when your phone turns off, so you'll need to setup the alarm again when your phone restarts. and you can then do this in your MyScheduleReceiver.