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>

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

  1. Sdcard插拔、状态广播监听,Android文件系统,Android存储器相关知识总结

    一 SDcard广播监听,注册,取消注册的实现 (1)根据实际需要监听的事件,添加action,并注册,一般在onCreate中添加 //在IntentFilter中选择你要监听的行为 IntentF ...

  2. Android短信监听实现,及Android4.4之后短信机制变更

    前阵子公司有一个项目,简单的监听短信应用,功能只有如下两个: 1.监听短信并获取短信内容上传服务器: 2.从服务器获取短信内容,发送出去    按照传统的思路,监听短信我们有两种方式:第一种是使用广播 ...

  3. Android实现网络监听

    一.Android Wifi常用广播 网络开发中主体会使用到的action: ConnectivityManager.CONNECTIVITY_ACTION WifiManager.WIFI_STAT ...

  4. Android 手势水平监听判断

    package com.zihao.ui; import com.zihao.R; import android.os.Bundle; import android.app.Activity; imp ...

  5. Android手机上监听短信的两种方式

    Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...

  6. android的电话监听

    android的电话监听 新建一个项目,结构图如下: PhoneService: package com.demo.tingdianhua; import android.app.Service; i ...

  7. Android零基础入门第34节:Android中基于监听的事件处理

    原文:Android零基础入门第34节:Android中基于监听的事件处理 上一期我们学习了Android中的事件处理,也详细学习了Android中基于监听的事件处理,同时学会了匿名内部类形式,那么本 ...

  8. Android中如何监听GPS开启和关闭

    转自 chenming 原文 Android中如何监听GPS开启和关闭   摘要: 本文简单总结了如何监听GPS开关的小技巧 有时需要监听GPS的开关(这种需求并不多见).实现的思路是监听代表 GPS ...

  9. 【Android测试】【随笔】模拟长按电源键

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...

  10. android应用程序监听SMS Intent广播

    当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent. 对于应用程序监听SMS Intent广播,首先需 ...

随机推荐

  1. Flutter状态管理新的实践

    1 背景介绍 1.1 声明式ui 声明式UI其实并不是近几年的新技术,但是近几年声明式UI框架非常的火热.单说移动端,跨平台方案有:RN.Flutter.iOS原生有:SwiftUI.android原 ...

  2. 5. Mybatis获取参数值的两种方式

    ‍ MyBatis 获取参数值的两种方式:​${} 和 #{}​ ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字段进行赋值 ...

  3. CPU性能指标介绍及分析

    CPU是计算机系统中最核心的组件之一,对系统性能起着至关重要的作用.以下是一些常见的CPU性能指标及其分析: 1. %user(用户态)和 %system(内核态) %user:表示CPU花费在用户进 ...

  4. 【Spring boot】 @Value注解

    一.不通过配置文件的注入属性 1.1 注入普通字符串 直接附在属性名上,在 Bean 初始化时,会赋初始值 @Value("normal") private String norm ...

  5. 加密流量识别检测(一)——在VM虚拟机上搭建指定拓扑

  6. msvc C++编译链接

    C++编译链接 C++编译链接 静态库编译 C RunTimeLibrary 链接过程 动态库编译 场景问题加深理解 总结 静态库编译 C RunTimeLibrary C++是C的超集,C RunT ...

  7. PaddleSharp:跨越一年的版本更新与亮点

    PaddleSharp:跨越一年的版本更新与亮点 我始终坚信,开源社区是技术进步的重要推动力,也是我抽出我业余时间,投入到PaddleSharp这个项目的原因,这个项目充分展现了.NET在复杂计算领域 ...

  8. Java并发(十三)----共享存在的问题

    1.小故事 老王(操作系统)有一个功能强大的算盘(CPU),现在想把它租出去,赚一点外快 小南.小女(不同的线程)来使用这个算盘来进行一些计算,并按照时间给老王支付费用 但小南不能一天24小时使用算盘 ...

  9. 层叠样式表(CSS)3

    三.层叠样式表属性 1.文字属性 font-size:字体大小 line-height:行高 font-family:字体 font-weight:粗细程度 .......等等很多,可自行学习 2.文 ...

  10. 【技术积累】Linux中的命令行【理论篇】【一】

    7z命令 命令介绍 7z命令是Linux系统中的一个压缩和解压缩工具,它可以用来创建.压缩和解压缩7z格式的文件.7z是一种高压缩率的文件格式,通常比其他常见的压缩格式(如zip和gzip)具有更高的 ...