单击电源按钮时激活应用程序

Hun*_*unt 7 android

我想在android中创建这样的应用程序,当我按两次睡眠或电源按钮时,它将被激活,是否可以通过在后台运行应用程序并从电源按钮收听事件来实现?

有时候一旦空闲电话进入睡眠模式,并且使用任何应用程序用户必须按下睡眠按钮然后他必须输入某些密码来激活电话.但我想让它在没有任何其他干预的情况下点击电源按钮时激活我的应用程序

Din*_*rma 13

你可以尝试这个技巧.

注册单击电源按钮时启动的广播接收器.现在在接收器的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>
Run Code Online (Sandbox Code Playgroud)

&&在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
    }
}
Run Code Online (Sandbox Code Playgroud)

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