Android电源按钮按下接收器无法接收

时间:2014-05-09 08:00:58

标签: android broadcastreceiver intentfilter

我正在尝试按下电源键按键,但我无法接收它。

接收器。

public class PowerSendMessage extends BroadcastReceiver
{
public PowerSendMessage(Activity activity) 
    {
        this.activity=activity;
    }
static int countPowerOff = 0;
private Activity activity = null;
@Override
public void onReceive(Context context, Intent intent) 
{
    Log.d("Power Button", "Click");

    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
    {
        countPowerOff++;
    }
    else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
    {
        if(countPowerOff == 2)
        {
            Log.d("Power Click", "2 Times");
        }
    }
}

清单条目

<receiver android:name=".PowerSendMessage">

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

放在下面的代码(意味着在哪个文件中)

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    PowerSendMessage mReceiver = new PowerSendMessage(this);
    registerReceiver(mReceiver, filter);

此程序是否需要任何许可?

我想我错过了一些东西,但我不知道的事情请帮助我。

1 个答案:

答案 0 :(得分:10)

试试这个

<强> MyReceiver.java

public class MyReceiver extends BroadcastReceiver 
{
    private static int countPowerOff = 0;

    public MyReceiver ()
    {

    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {    
            Log.e("In on receive", "In Method:  ACTION_SCREEN_OFF");
            countPowerOff++;
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
            Log.e("In on receive", "In Method:  ACTION_SCREEN_ON");
        }
        else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
            Log.e("In on receive", "In Method:  ACTION_USER_PRESENT");
            if (countPowerOff > 2)
            {
                countPowerOff=0;
                Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
                Intent i = new Intent(context, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            }
        }
    }
}

<强> MainActivity.java

public class MainActivity extends Activity 
{
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy() 
    {
        if (myReceiver != null)
        {
            unregisterReceiver(myReceiver);
            myReceiver = null;
        }           
        super.onDestroy();
    }
}

如果您已在活动中注册过接收器,则无需在清单中注册接收器。

此外,最好从ACTION_USER_PRESENT开始您的活动,因为这意味着设备在按下电源按钮锁定后已解锁。但是,如果您想从ACTION_SCREEN_ON启动活动,您也可以这样做,这也可以,但只有在用户解锁设备后,活动才会显示。

修改

如果你想从外部应用程序接收广播事件的接收器你可以使用服务

首先,与其他广泛的意图不同,对于Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON,您不能在Android Manifest中声明它们!所以你需要提供一个像这样继续运行的服务

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

你的接收器可能是什么

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

}

您需要添加 <service android:name=". UpdateService"/>

并通过

从活动中运行此服务
context.startService(new Intent(this, UpdateService.Class));

以下是完整示例,符合您的要求

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/