捕获方法说明
1.Intent.ACTION_CLOSE_SYSTEM_DIALOGS和Intent.ACTION_TIME_TICK不能再Manifest中注册使用,必须在程序中动态注册
2.Intent.ACTION_TIME_TICK按照时钟刻度,即开始下一分钟时自动发送广播
3.这2种广播属于“程序”级别的广播,不能作为app级别的广播使用
二、代码实战
捕获HomeKey与TIME_TICK
public class CapatureHomeKey extends BaseActivity {
HomeKeyEventBroadCastReceiver receiver;
@Override
protected void onCreate(Bundle saveInstance) {
super.onCreate(saveInstance);
getWindow().setBackgroundDrawableResource(R.drawable.avatar12);
receiver = new HomeKeyEventBroadCastReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(receiver,filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
private class HomeKeyEventBroadCastReceiver 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
private final String TAG = "HomeKeyTag";
@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 (SYSTEM_HOME_KEY.equals(reason))
{
Log.d(TAG, "--->"+SYSTEM_HOME_KEY); //捕获到Home键
}
else if (SYSTEM_RECENT_APPS.equals(reason))
{
Log.d(TAG, "--->"+SYSTEM_RECENT_APPS); //捕获到最近打开的Activity
}
}else if (action.equals(Intent.ACTION_TIME_TICK)) {
//to do
}
}
}
}