当前位置: 移动技术网 > IT编程>移动开发>Android > Android监听home键的方法详解

Android监听home键的方法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

黑衣人1高清下载,松木价格,斗战神狸猫刀卫学徒

本文实例分析了android监听home键的方法。分享给大家供大家参考,具体如下:

如何知道home按钮被点击了呢?做launcher的时候,看源代码发现原因

如果你的activity具备这些属性

<activity
 android:name="com.woyou.activity.homeactivity"
 android:launchmode="singleinstance" >
 <intent-filter>
  <action android:name="android.intent.action.main" />
  <category android:name="android.intent.category.default" />
  <category android:name="android.intent.category.home" />
  <category android:name="android.intent.category.launcher" />
 </intent-filter>
</activity>

当系统点击home按键的时候,系统会向具有这些属性的activity发出intent

然后你重写activity的onnewintent方法

这个方法就会回调onnewintent这个方法

已验证可用!

下面这个是我重新写的监听home键的方式,以前写的那些方式都不是很好用。现在的这种方式通过广播的方式监听home键,这个比较好使

1.首先是创建一个广播接受者

private broadcastreceiver mhomekeyeventreceiver = new broadcastreceiver() {
  string system_reason = "reason";
  string system_home_key = "homekey";
  string system_home_key_long = "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_reason);
    if (textutils.equals(reason, system_home_key)) {
      //表示按了home键,程序到了后台
     toast.maketext(getapplicationcontext(), "home", 1).show();
    }else if(textutils.equals(reason, system_home_key_long)){
     //表示长按home键,显示最近使用的程序列表
    }
   }
  }
};

2.注册监听

可以在activity里注册,也可以在service里面

//注册广播
registerreceiver(mhomekeyeventreceiver, new intentfilter(
  intent.action_close_system_dialogs));

完整的代码如下:

package com.example.homedemo;
import android.os.bundle;
import android.app.activity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.text.textutils;
import android.view.menu;
import android.widget.toast;
public class mainactivity extends activity {
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  //注册广播
  registerreceiver(mhomekeyeventreceiver, new intentfilter(
    intent.action_close_system_dialogs));
 }
 /**
  * 监听是否点击了home键将客户端推到后台
  */
 private broadcastreceiver mhomekeyeventreceiver = new broadcastreceiver() {
  string system_reason = "reason";
  string system_home_key = "homekey";
  string system_home_key_long = "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_reason);
    if (textutils.equals(reason, system_home_key)) {
      //表示按了home键,程序到了后台
     toast.maketext(getapplicationcontext(), "home", 1).show();
    }else if(textutils.equals(reason, system_home_key_long)){
     //表示长按home键,显示最近使用的程序列表
    }
   }
  }
 };
}

下面是以前写的监听方式,现在大部分都不好使了,这次作为更新:

监听home键一直困扰这大家,也是让大家非常纠结的问题,从我的文章点击量上看,就知道这个问题有多么难以解决。

这里又来了一次修改,第一部分是解决2.2或者之前系统版本home的监听,第二部分是4.0.x的home监听

第一部分:

如果想监听home键,实现方式有几种

第一种方式:android 对home键的监听,加上了权限,必须取得对处理home键事件的权限,才能对home键进行操作,

只对2.2及以前的系统有效。

1,加上权限

<uses-permission 
android:name="android.permission.disable_keyguard"/>

就是让键盘守卫失去能了,根据英文大体是这个意思

2,重载以下两个方法@override

public boolean onkeydown(int keycode, keyevent event) {
 if(keyevent.keycode_home==keycode){
  //写要执行的动作或者任务
   android.os.process.killprocess(android.os.process.mypid());
 }
 return super.onkeydown(keycode, event);
}
@override
public void onattachedtowindow(){
  this.getwindow().settype(windowmanager.layoutparams.type_keyguard);
  super.onattachedtowindow();
}

第二种方式:可能需要修改源代码,修改系统的源码,这样比较麻烦,不建议使用,就用第一种方式就行了

还有其他变通的是实现方式,就是根据自己的业务,可以判断出点击了home键。

由于这篇文章写的比较早,没有测试过2.3和之后的版本,书写有误,在此抱歉。之后再想监听home键应该只能修改源码了。对android研究比较深的朋友可以尝试一下。找到屏蔽home键的地方,给我私信,谢谢。希望咱们大家能把这个纠结于很多程序员的问题给解决了,算是给程序员减少点纠结的工作,大家一起努力解决吧。

第二部分:

第一部分的方式一的

代码移植到4.0.1后 this.getwindow().settype(windowmanager.layoutparams.type_keyguard); 这行报错,

错误提示:java.lang.illegalargumentexception: window type can not be changed after the window is added。

可以有两种处理方式

方法一、修改phonewindowmanager.java

if (keycode == keyevent.keycode_home) {
 //在这里发送一个广播出去
}

在应用里接收到广播后做相应处理,一般是做系统时才会去改源码。

方法二、检测log,根据log判断是否有点击home键

private boolean istesting = true;
class catchlogthread extends thread {
 @override
 public void run() {
  process mlogcatproc = null;
  bufferedreader reader = null;
  string line;
  while (istesting) {
   try {
    // 获取logcat日志信息
    mlogcatproc = runtime.getruntime().exec(new string[] { "logcat", "activitymanager:i *:s" });
    reader = new bufferedreader(new inputstreamreader(mlogcatproc.getinputstream()));
    while ((line = reader.readline()) != null) {
     if (line.indexof("android.intent.category.home") > 0) {
      istesting = false;
      handler.sendmessage(handler.obtainmessage());
      runtime.getruntime().exec("logcat -c");//删除日志
      break;
     }
    }
   } catch (exception e) {
    e.printstacktrace();
   }
  }
 }
};
handler handler = new handler() {
 public void handlemessage(android.os.message msg) {
  log.i(tag,"home key press");
  //do something here
 };
};

这个方式出自这篇文章《android 4.0.x home键事件拦截/监听
获取log的方法:《android开发之在程序中时时获取logcat日志信息的方法

这个方式先在这里分享给大家,经过测试之后,再修改在修改一下,到时候用的时候方便

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网