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. Dash应用页面整体布局技巧

    本文示例代码已上传至我的Github仓库:https://github.com/CNFeffery/dash-master 大家好我是费老师,对于刚上手dash应用开发的新手朋友来说,如何进行合理且美 ...

  2. AI隐私保护中的常见隐私隐私问题与解决方案

    目录 题目:<AI隐私保护中的常见隐私问题与解决方案> 引言 随着人工智能技术的快速发展,AI隐私保护也成为了一个备受关注的问题.由于AI技术的应用范围越来越广泛,例如语音识别.图像识别. ...

  3. 我坚定的认为,这个源码肯定是有 BUG 的!

    你好呀,我是歪歪. 上周我不是发了<我试图给你分享一种自适应的负载均衡.>这篇文章嘛,里面一种叫做"自适应负载均衡"的负载均衡策略,核心思路就是从多个服务提供者中随机选 ...

  4. 使用GoEasy快速实现Android原生app中的websocket消息推送

    摘要: GoEasy带来了一项令开发者振奋的消息:全面支持Android原生平台!现在,您可以在Android应用中使用最酷炫的实时通信功能,借助GoEasy轻松实现消息的发送和接收.本文将带您领略G ...

  5. 堆栈式 CMOS、背照式 CMOS 和传统 CMOS 传感器的区别

    光电效应 光电效应的现象是赫兹(频率的单位就是以他命名的)发现的,但是是爱因斯坦正确解释的.简单说,光或某一些电磁波,照射在某些光敏物质会产生电子,这就是光电效应. 这就将光变为了电,光信号的改变会带 ...

  6. win10安装mysql时提示错误:mysqld: Can't change dir to 'C: oftware\mysql\data\' (Errcode: 2 - No such file or directory)

    win10安装解压版mysql时,提示错误: 2019-10-22 09:02:00 2004 [ERROR] Can't find messagefile 'C:\WINDOWS\system32\ ...

  7. 前端Vue自定义开屏启动广告组件,点击广告图跳转广告详情

    随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身. 通过组件化开发,可以有 ...

  8. c# 文件在线预览功能

    using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using DocumentF ...

  9. vue + canvas 实现涂鸦面板

    前言 专栏分享:vue2源码专栏,vue router源码专栏,玩具项目专栏,硬核 推荐 欢迎各位 ITer 关注点赞收藏 此篇文章用于记录柏成从零开发一个canvas涂鸦面板的历程,最终效果如下: ...

  10. Linq开发技巧与业务逻辑校验

    Linq 是一种基于 .NET Framework 的编程语言,它的出现极大地提高了开发效率.Linq 提供了一种统一的查询语法,使得开发人员可以使用一种语言来查询不同类型的数据源,包括对象.集合.数 ...