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

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

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

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

}
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">

<!--  main.xml-->

    <textview android:layout_centerinparent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="利用广播监听Home键和电源键">

</textview></relativelayout>
时间: 2024-10-25 15:39:46

Android利用广播监听按下HOME和电源键的相关文章

Android -- 使用广播监听Home键

//广播监听事件类 public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //do what you want System.exit(0); } } private HomeKeyEventBroadCastReceiver homeKeyEventReceiver; prote

求助 利用广播监听 手机拨号 在虚拟机上能运行 真机没法应

部分代码 如下   求大神 解答 public class OutgoingCallReceiver extends BroadcastReceiver { @SuppressLint("ShowToast") @Override public void onReceive(Context context, Intent intent) { String phone = this.getResultData();//得到外拔电话 System.out.println("拨号&

android 通过广播监听网络连接状况

首先添加以下权限 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE&quo

Android实现广播监听HOME键操作

先写了个Service,在服务中通过广播来监听HOME键操作: public class HomeService extends Service{ private MonitoHomeReceiver mHomeBroadcastReceiver; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mHomeBro

Android短信监听(一)——利用BroadcastReceiver实现短信监听

MainActivity如下: package cc.testsmsbroadcastreceiver; import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用BroadcastReceiver实现

Android短信监听(三)——利用Loader实现短信监听

MainActivity如下: package cc.c; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; import android.content.Loader; import android.database.Curs

Android短信监听(二)——利用ContentObserver实现短信监听

MainActivity如下: package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.app.Activity; /** *

Android开发之使用广播监听网络状态变化

我们经常需要判断网络状态的变化,如有无网络,所以需要监听网络状态的变化,比如网络断开,网络连接给予友好提示.如何监听网络状态的变化呢,最近工作中需要用到这个,于是就用广播机制来实现了网络状态的监听. 使用广播机制来监听网络状态主要涉及到以下几点: 1.需要自己编写个网络广播接收器,该接收器继承BroadcastReceiver类,用来接收系统发出的广播.比如我自己写了个NetStatusReceiver广播接收器.代码如下: package com.log.system; import andr

android应用程序监听SMS Intent广播

当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent. 对于应用程序监听SMS Intent广播,首先需要添加RECEIVE_SMS权限.通过在应用程序manifest中添加一个uses-permission,如下面的片段所示: <uses-permission android:name="android.permission.RECEIVE_SMS"/> AndroidMani