单击电源button时激活应用程序

我想在Android中创build这样的应用程序将被激活,当我按睡眠或电源button两次,是否有可能做到这一点,通过在后台运行一个应用程序和从电源button聆听事件?

有些时候电话一进入hibernate模式就进入睡眠模式,使用任何应用程序的用户必须按睡眠键,然后input一定的密码才能激活电话。 但是,我想让它激活我的应用程序时,单击电源button没有任何其他干预

你可以试试这个技巧。

注册点击电源button时启动的广播接收器。 现在在Receiver的OnReceive方法中做你想做的事情。

例如:

在清单文件中注册一个接收器:

<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> 

在Receiver的onReceive()方法中&&

  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 } } 

现在在Receiver的onReceive()方法中执行任何操作。

这是一个权力下降事件的权力,你可以从这个只是尝试一些想法,你会得到一些想法…我没有尝试这样的行为,但我发现这个权力经理。

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) { // do what you want with the power button return true; } return super.onKeyDown(keyCode, event); }