I am working on the application,I am having the requirement:if user long press the power button at any time and press again to start device,the activity should start from where the device was shut down.I dont know either this is a valid question or not.
I tried working with:
public class PowerMangerTestActivity extends Activity {
private static PowerManager objpowermanager;
private static PowerManager.WakeLock wl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callTOWakeLock();
setContentView(R.layout.main);
}//end of onCreate
public void callTOWakeLock() {
// TODO Auto-generated method stub
objpowermanager=(PowerManager)getSystemService(Context.POWER_SERVICE);
wl=objpowermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "******MyTag****");
//You should acquire it when your app starts,
if(wl.isHeld())
{
wl.release();
}
wl.acquire();
}
@Override
public void onDestroy() {
wl.release();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
callTOWakeLock();
}
/*@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, PowerMangerTestActivity.class);
startActivity(i);
return true;
TextView tv=(TextView) findViewById(R.id.textview);
tv.setText("You press power button");
}
return super.dispatchKeyEvent(event);
}*/
}
but not getting expected behaviour.
I have tried to catch KeyEvent.KEYCODE_POWER,but not getting how to use for this scenario.
any suggestions?
thanks
This may be unacceptable to you, but you should NOT DO THIS.
It is going against a usability idea called “expected behavior”.
You are doing something that a user is not expecting, and there is a good chance they are going to be pissed if you do this.
Just my two cents!
I would try and find another solution to this problem, such as caching useful data in phone storage, and recalling it when the application is resumed/started.
onPause still should be called when the power button is long pressed. The only case (as far as I know) where it won’t be called is on a battery pull