tags:

views:

42

answers:

2

Does anyone know if it is possible to force an app to start automatically when external power is applied - i.e. the following scenario: - app is running (with permanent external power supply; this would not be a 'mobile' app); - external supply fails for some reason; - battery runs low so device turns off; - external supply resumes; - app must start automatically, without pressing the home button or touching the screen, since the device would be unattended. Would this action be different depending on manufacturer's hardware?

A: 

I think you can receive a broadcast when the power cable is plugged in/out. I'm not sure what the exact message is. I'm sure someone else can post that or you can google it.

Falmarri
+1  A: 

From your description, your application is always running, so why would you want to start when external power is applied?

If your Android device really shuts down, then I assume you want to start your application on boot. In that case, you need to write an Android BroadcastReceiver that receives the intent android.intent.action.BOOT_COMPLETED. When your receiver is called, you can start whatever application you want in the onReceive(Context,Intent) method.

Remember to declare the receiver in your manifest and also add the permission android.permission.RECEIVE_BOOT_COMPLETED.

Check out the Android Competency Centre for an example.

P.S. If you really want to do something when power source changes, then you might want to listen for the Intent "android.intent.action.ACTION_POWER_CONNECTED and related Intents.

codinguser
+1 for android.intent.action.ACTION_POWER_CONNECTED
Falmarri
Thanks codinguser, sounds as if this will be realtively easy to achieve. In answer to your initial question, I am assuming the worst, i.e.that when both external supply fails and battery runs down, then any running app would be killed off.
chris fernley