0

Is it possible to force a reboot of the device after my apk is installed?

I want to force this because I want to ensure that my service is started.

Felix
  • 88,392
  • 43
  • 149
  • 167
user902131
  • 11
  • 1

2 Answers2

2

Most probably the answer is no, your are not allowed to do such things from your app. This is the sole privilege of the user holding the phone (and of maybe the core system services).

You can however ensure you service is started when the user starts you main activity, which would be a very normal thing to do right after the user have installed your application.

For additional information see the question How to start android service on installation, which is in fact what you should be trying to do.

Community
  • 1
  • 1
Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
  • ok, so is it maybe able to leave a message after install, to show the user that he must restart? – user902131 Aug 24 '11 at 09:27
  • 1
    Why would you want the user to reboot the phone? Are you not just trying to ensure that you service is started? If so, just leave it up to the user to start your application when he needs it (and in consequence your service.) – Bjarke Freund-Hansen Aug 24 '11 at 09:29
0

It's not possible in any way to get your application to do anything as soon as it's installed, before the user first launches it from the home screen. There's no broadcast action you can listen for explicitly. However, you can listen for something generic that gets called a lot, such as:

  • android.intent.action.USER_PRESENT,
  • android.intent.action.SCREEN_OFF, or
  • android.intent.action.SCREEN_ON

In any case you should NOT reboot the device. Your users will hunt you down and kill you with stones. Joke aside, Google might actually pull your app from the Market for this. Just listen for one of the actions mentioned above, check if the app has just been installed (using a one-time boolean preference, for example) and start the service.

Note: if you do end up listening for one of the above actions, please disable your receiver the first time it receives an intent. You can do this like so (in your receiver):

public class FirstTimeReceiver extends BroadcastReceiver {

    public void onReceive (Context context, Intent intent) {
        // start your service (which does stuff asynchronously, of course, and then:
        final ComponentName mySelf = new ComponentName(context, FirstTimeReceiver.class);
        context.getPackageManager().setComponentEnabledSetting(mySelf, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }

}

However, you should only do this if somehow this service is absolutely critical for the user (there are few proper scenarios for this), and not for you / your app. As bjarkef mentioned, you should only start it after the user starts your app from the home screen (better yet, ask for permission from the user to run the service).

Felix
  • 88,392
  • 43
  • 149
  • 167
  • thx 4 ur help. I will try this way without reboot :-) you are right it is better for the user – user902131 Aug 24 '11 at 09:34
  • Does TIME_TICK broadcast intent could be a good way to start the service ASAP ? – Emmanuel Devaux Aug 24 '11 at 09:37
  • 1
    Remember to unregister the receiver immediately after starting the service on an intent like this, as to not unnecessarily slow the users phone down. – Bjarke Freund-Hansen Aug 24 '11 at 09:49
  • 1
    @Emmanuel if you would look at [the docs](http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK) you would see that you can only register such a receiver through code (if you try to declare it in the manifest it will be ignored). – Felix Aug 24 '11 at 10:23
  • @bjarkef thanks for the tip, I added an example of how to do that. – Felix Aug 24 '11 at 10:33