一:Android 中Home键监听和Back键监听的区别: (1).在Android中,当按下Home键的时候,默认情况下Stop前台的Activity,即Activity设
一:Android 中Home键监听和Back键监听的区别:
(1).在Android中,当按下Home键的时候,默认情况下Stop前台的Activity,即Activity设置成停止状态【onStop()】,而不是销毁状态【onDestory()】。如果再次启动该Activity,不是调用onCreate()方法,而是调用onSavedInstanceState方法。则是从onRestart()开始-onStart()-onResume()。
(2).当按下back键则不同,back键默认finish前台的activity,即activity的状态为onDestory为止,再次启动该activity则从onCreate开始,不会调用onSavedInstanceState方法。
总而言之:按下Home键会返回桌面,而back则会返回前一个activity。
在这里Back键的实现方法就不赘述了,主要讲解一下Home键的实现方式吧。以下是我找的参考资料自己思考的。请大家给出建议。
Home键监听的实现方法:
(1).在onResum里面注册广播,OnPause里面注销广播。
(2).在广播中拦截Intent.ACTioN_CLOSE_SYSTEM_DIALOGS 这个Action ,通过获取Reason字段 来判断长按 还是单击Home键。
代码如下:
(1).Home键监听封装类:
package com.scd.homewatcher.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class HomeWatcher {
// 回调接口
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
}
private static final String TAG = "HomeWatcher";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerRecevier mRecevier;
public HomeWatcher(Context context) {
mContext = context;
mRecevier = new InnerRecevier();
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}
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);
}
}
private 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.i(TAG, "action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
// 短按home键
mListener.onHomePressed();
} else if (reason
.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
// 长按home键
mListener.onHomeLongPressed();
}
}
}
}
}
}
}
(2).MainActivity类:
package com.scd.homewatcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
import com.scd.homewatcher.util.HomeWatcher;
import com.scd.homewatcher.util.HomeWatcher.OnHomePressedListener;
public class MainActivity extends Activity implements OnHomePressedListener {
private HomeWatcher mHomeWatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(this);
// 注册广播
mHomeWatcher.startWatch();
super.onResume();
}
@Override
protected void onPause() {
mHomeWatcher.setOnHomePressedListener(null);
// 注销广播
mHomeWatcher.stopWatch();
super.onPause();
}
@Override
public void onHomePressed() {
// TODO
Toast.makeText(this, "短按Home键,实现自己的逻辑", Toast.LENGTH_SHORT).show();
}
@Override
public void onHomeLongPressed() {
// TODO
Toast.makeText(this, "长按Home键,实现自己的逻辑", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("按下了back键 onKeyDown()");
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
public void onBackPressed() {
// super.onBackPressed()会自动调用finish()方法,关闭
super.onBackPressed();
}
}
您可能感兴趣的文章:Android 屏蔽和捕获Home键的示例代码Android监听home键的方法详解Android中监听Home键的4种方法总结android4.0屏蔽HOME键的简单实现Android应用开发中模拟按下HOME键的效果(实现代码)android里面屏蔽home键/禁止Home键或者随你DIYAndroid 中按home键和跳转到主界面的实例代码
--结束END--
本文标题: Android监听Home键和Back键的区别介绍
本文链接: https://lsjlt.com/news/26641.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0