android电源键广播,Android利用广播监听按下HOME和电源键

MainActivity如下:

package cc.testhome;

import cc.testhome.HomeKeyObserver.OnHomeKeyListener;

import cc.testhome.PowerKeyObserver.OnPowerKeyListener;

import android.os.Bundle;

import android.app.Activity;

/**

* Demo描述:

* 利用广播监听Home键的按下和长按Home键

* 利用广播监听电源键的按下(关闭屏幕)

*

* 参考资料:

* 1 http://blog.csdn.net/q445697127/article/details/8432513

* 2 http://blog.csdn.net/watt520/article/details/18959897

* 3 http://blog.csdn.net/lfdfhl/article/details/9903693

* Thank you very much

*/

public class MainActivity extends Activity {

private HomeKeyObserver mHomeKeyObserver;

private PowerKeyObserver mPowerKeyObserver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

init();

}

private void init() {

mHomeKeyObserver = new HomeKeyObserver(this);

mHomeKeyObserver.setHomeKeyListener(new OnHomeKeyListener() {

@Override

public void onHomeKeyPressed() {

System.out.println("----> 按下Home键");

}

@Override

public void onHomeKeyLongPressed() {

System.out.println("----> 长按Home键");

}

});

mHomeKeyObserver.startListen();

//

mPowerKeyObserver = new PowerKeyObserver(this);

mPowerKeyObserver.setHomeKeyListener(new OnPowerKeyListener() {

@Override

public void onPowerKeyPressed() {

System.out.println("----> 按下电源键");

}

});

mPowerKeyObserver.startListen();

}

@Override

protected void onDestroy() {

super.onDestroy();

mHomeKeyObserver.stopListen();

//

mPowerKeyObserver.stopListen();

}

}

HomeKeyObserver如下:

package cc.testhome;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

public class HomeKeyObserver {

private Context mContext;

private IntentFilter mIntentFilter;

private OnHomeKeyListener mOnHomeKeyListener;

private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver;

public HomeKeyObserver(Context context) {

this.mContext = context;

}

//注册广播接收者

public void startListen(){

mIntentFilter=new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

mHomeKeyBroadcastReceiver=new HomeKeyBroadcastReceiver();

mContext.registerReceiver(mHomeKeyBroadcastReceiver, mIntentFilter);

System.out.println("----> 开始监听");

}

//取消广播接收者

public void stopListen(){

if (mHomeKeyBroadcastReceiver!=null) {

mContext.unregisterReceiver(mHomeKeyBroadcastReceiver);

System.out.println("----> 停止监听");

}

}

// 对外暴露接口

public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) {

mOnHomeKeyListener = homeKeyListener;

}

// 回调接口

public interface OnHomeKeyListener {

public void onHomeKeyPressed();

public void onHomeKeyLongPressed();

}

//广播接收者

class HomeKeyBroadcastReceiver extends BroadcastReceiver{

final String SYSTEM_DIALOG_REASON_KEY = "reason";

//按下Home键

final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

//长按Home键

final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {

String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

if (reason != null && mOnHomeKeyListener != null) {

if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {

mOnHomeKeyListener.onHomeKeyPressed();

} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {

mOnHomeKeyListener.onHomeKeyLongPressed();

}

}

}

}

}

}

PowerKeyObserver如下:

package cc.testhome;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

public class PowerKeyObserver {

private Context mContext;

private IntentFilter mIntentFilter;

private OnPowerKeyListener mOnPowerKeyListener;

private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver;

public PowerKeyObserver(Context context) {

this.mContext = context;

}

//注册广播接收者

public void startListen(){

mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF);

mPowerKeyBroadcastReceiver=new PowerKeyBroadcastReceiver();

mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter);

System.out.println("----> 开始监听");

}

//取消广播接收者

public void stopListen(){

if (mPowerKeyBroadcastReceiver!=null) {

mContext.unregisterReceiver(mPowerKeyBroadcastReceiver);

System.out.println("----> 停止监听");

}

}

// 对外暴露接口

public void setHomeKeyListener(OnPowerKeyListener powerKeyListener) {

mOnPowerKeyListener = powerKeyListener;

}

// 回调接口

public interface OnPowerKeyListener {

public void onPowerKeyPressed();

}

//广播接收者

class PowerKeyBroadcastReceiver extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (action.equals(Intent.ACTION_SCREEN_OFF)) {

mOnPowerKeyListener.onPowerKeyPressed();

}

}

}

}

main.xml如下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_centerInParent="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="利用广播监听Home键和电源键" />



原文:http://blog.csdn.net/lfdfhl/article/details/19017319

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值