技术联盟

android Home键监听

监听工具类

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class HomeWatcher {
static final String TAG = “HomeWatcher”;
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerRecevier mRecevier;

// 回调接口
public interface OnHomePressedListener {
public void onHomePressed();

public void onHomeLongPressed();
}

public HomeWatcher(Context context) {
mContext = context;
mRecevier = new InnerRecevier();
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}

/**
* 设置监听
*
* @param listener
*/
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
}

/**
* 开始监听,注册广播
*/
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}

/**
* 停止监听,注销广播
*/
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}

/**
* 广播接收者
*/
class InnerRecevier extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = “reason”;
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = “globalactions”;
final String SYSTEM_DIALOG_REASON_RECENT_APPS = “recentapps”;
final String SYSTEM_DIALOG_REASON_HOME_KEY = “homekey”;

@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) {
Log.e(TAG, “action:” + action + “,reason:” + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
// 短按home键
mListener.onHomePressed();
Intent intents =new Intent(context,MainActivity.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intents);
} else if (reason
.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
// 长按home键
mListener.onHomeLongPressed();
}
}
}
}
}
}
}

 

 

测试Activity

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity implements OnHomePressedListener {
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
private HomeWatcher mHomeWatcher = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED,
FLAG_HOMEKEY_DISPATCHED);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onResume() {
mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(this);
mHomeWatcher.startWatch();
super.onResume();
}

@Override
protected void onPause() {
super.onPause();
mHomeWatcher.setOnHomePressedListener(null);
mHomeWatcher.stopWatch();
}

@Override
public void onHomePressed() {
Log.e(“onHomePressed”, “onHomePressed”);

}

@Override
public void onHomeLongPressed() {
Log.e(“onHomeLongPressed”, “onHomeLongPressed”);
}
}

Share this:

码字很辛苦,转载请注明来自技术联盟《android Home键监听》

评论