激活应用程序被点击电源按钮时 [英] activate an application when a power button is clicked

查看:77
本文介绍了激活应用程序被点击电源按钮时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建的Andr​​oid这样的应用程序时,我preSS睡眠或两次电源按钮将被激活,是有可能做到这一点,通过在后台运行的应用程序,并聆听来自电源按钮事件?

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?

有时电话进入睡眠模式,一旦它处于闲置状态,以及使用的任何应用程序用户已到preSS 睡眠按钮,然后他要进入某些密码来激活手机。但我想让使其激活我的应用程序点击没有任何其他干预的电源按钮时

some times phone gets into sleep mode once it is idle , and to use any application user has to press sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention

推荐答案

您可以尝试这一招。

注册一个广播接收器powerbutton被点击时被启动。 现在,在接收器的onReceive方法做你想要什么。

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

例如:

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

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>

&功放;&安培;在的onReceive()接收器的方法

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

现在的onReceive()接收器的方法执行任何操作。

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

这篇关于激活应用程序被点击电源按钮时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆