当前位置: 代码迷 >> Android >> Android中中擒获HOME事件
  详细解决方案

Android中中擒获HOME事件

热度:104   发布时间:2016-04-28 07:38:19.0
Android中中捕获HOME事件

Android中中捕获HOME事件,虽然网上有很多例子。但我照着弄大多不成功。在此重新写一个例子。

首先,创建一个接收广播的类

HLHomeKeyEventBroadCastReceiver.java

package com.ldci.djrotatemenuforandroid;import com.huluo.welcome.HLWelcomeActivity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class HLHomeKeyEventBroadCastReceiver extends BroadcastReceiver{	 static final String SYSTEM_REASON = "reason";  	    static final String SYSTEM_HOME_KEY = "homekey";//home key  	    static final String SYSTEM_RECENT_APPS = "recentapps";//long home key  			@Override	public void onReceive(Context context, Intent intent) {						// TODO Auto-generated method stub		 String action = intent.getAction();  	        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {  	            String reason = intent.getStringExtra(SYSTEM_REASON);  	            if (reason != null) {  	                if (reason.equals(SYSTEM_HOME_KEY)) {  	                    // home key处理点  	                		                	//停止服务	                	context.stopService(HLWelcomeActivity.m_intent_RotateMenuIntent);	                 	Log.i("vvvvvvvvvv", "mmmmmm");	                		                } else if (reason.equals(SYSTEM_RECENT_APPS)) {  	                    // long home key处理点  	                }  	            }  	        }  	}	}

在程序的入口类中添加如下代码:

HLHomeKeyEventBroadCastReceiver receiver = new HLHomeKeyEventBroadCastReceiver();				IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);				registerReceiver(receiver, intentFilter);


至此点击Home事件即可捕捉。



  相关解决方案