Activate an Application When a Power Button Is Clicked

activate an application when a power button is clicked

You can try this trick .

Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.

For example:

in manifest file register a receiver:

 <receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>

&& in onReceive() method of the Receiver

 public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

Log.v("#@%@%#", "Power button is pressed.");

Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();

//perform what you want here
}
}

Now perform any operation in onReceive() method of the Receiver.

Controlling the Application when the Power Button is pressed and returning to the App

Most likely your activity is being killed whilst in the background. I imagine the Dell Streak is more prone to this as its an older device compared to the HD and probably has less RAM, im not sure how much any Dell customization takes up though (if any).

You probably dont want to be listening for the power button press specifically but rather hooking in the appropriate logic to the Activitys lifecycle methods, especially foccusing on the methods that are called when the activity is created and destroyed as opposed to paused and resumed.

What kind of data is being reset? Is this data being obtained remotly? There are a number of ways to combat this problem.

One is to preserve UI state when the Activity goes is destroyed, see onSaveInstanceState(Bundle)

See here regarding some more info regarding persiting data, which coveres Databases and Shared Prefs which are two alternatives to saving application state (or UI state in some circumstances ) to the onSaveInstanceState() method mentioned above. It really depends on what you are doing :)

Also, are there any remote data gathering operations being triggered in the lifecycle methods (like onCreate()) of this activity? There may be a better architecture for your app if so, say where the lifecycle method just triggers a conditional time-based refresh request thats wrapped in a Service (which is also less likely to be killed in the background) that will just update some model data (in mem or database backed) that the UI can be populated from, but has the advantage that its also persisted so that when your acitvity is killed it will be transparent to the user!

Hope this helps.

EDIT: i just thought, if you are saving state when the back button is pressed and using that saved state to reset the UI state when onResume or onStart is called, are you possibly just clearing the state completely if the activity was put into the background (but not killed) from any other action than pressing the back bytton, as your saved state object will be blank / null / contain default values? If so then the above approaches are still relavent

how to start the app on power button press

First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! so You need to make a service which will keep on running like this

public static class UpdateService extends Service {

@Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new Receiver();
registerReceiver(mReceiver, filter);
}

@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
// your code
} else {
// your code
}
}
}

and your receiver can be something

public class Receiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}

}


Related Topics



Leave a reply



Submit