主要是应用在Android嵌入式工程里
1.首先我们写工具类 拷贝就好 程序能跑就行
import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import com.bhwm.serialportmng.activity.MainActivity; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; //拦截键盘 public class HomeListener { private static final String TAG1 = "HomeListener"; public static HomeKeyListener getInstance() { HomeKeyListener listener = HomeKeyListener.sListener; listener.init(); return listener; } public static class HomeKeyListener { private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";//home键旁边的最近程序列表键 private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";//按下home键 private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";//某些三星手机的程序列表键 private static AtomicBoolean isDestroy = new AtomicBoolean(true); private static AtomicBoolean isRegister = new AtomicBoolean(false); private static HomeKeyListener sListener = new HomeKeyListener(); private List<HomePressListener> mPressListeners = new ArrayList<HomePressListener>(); private HomeReceiver mReceiver; private IntentFilter mHomeFileter; private HomePressListener mHomePressListener; public void init() { if (isDestroy.get()) { this.mPressListeners = new ArrayList<HomePressListener>(); this.mReceiver = new HomeReceiver(); this.mHomeFileter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); isDestroy.set(false); } } public synchronized void start(Context context) { if (!isRegister.get()) { context.registerReceiver(mReceiver, mHomeFileter); isRegister.set(true); } } public synchronized void stop(Context context) { if (isRegister.get()) { context.unregisterReceiver(mReceiver); } } public void setHomeKeylistener(HomePressListener listener) { this.mHomePressListener = listener; } public void addHomeKeyListener(HomePressListener listener) { mPressListeners.add(listener); } public void removeHomeKeyListener(HomePressListener listener) { mPressListeners.add(listener); } public void removeAllHomeKeyListener() { mPressListeners.clear(); } public void destroy() { this.mPressListeners.clear(); this.mPressListeners = null; this.mReceiver = null; this.mHomeFileter = null; this.mHomePressListener = null; isDestroy.set(true); } public interface HomePressListener { void onHomePress(); void onHomeRecentAppsPress(); } class HomeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 备注点击home键跳转位置 intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); try { pendingIntent.send(); } catch (Exception e) { context.startActivity(intent); } // receive(intent); } } private void receive(Intent intent) { String action = intent.getAction(); if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra("reason"); dispatchPress(reason); } } private void dispatchPress(String reason) { Log.e(TAG1, "reason------> " + reason); switch (reason) { case SYSTEM_DIALOG_REASON_HOME_KEY: if (mHomePressListener != null) mHomePressListener.onHomePress(); for (HomePressListener listener : mPressListeners) listener.onHomePress(); break; case SYSTEM_DIALOG_REASON_RECENT_APPS: case SYSTEM_DIALOG_REASON_ASSIST: if (mHomePressListener != null) mHomePressListener.onHomeRecentAppsPress(); for (HomePressListener listener : mPressListeners) listener.onHomeRecentAppsPress(); break; } } } }
2.第二步在activity里初始化
HomeListener.getInstance().start(this);
注:需要权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <uses-permission android:name="android.permission.REORDER_TASKS" /> <uses-permission android:name="android.permission.STOP_APP_SWITCHES" tools:ignore="ProtectedPermissions" />
android:sharedUserId="android.uid.system"
AndroidManifest.xml里的manifest里加 android:sharedUserId="android.uid.system" 如果不加也可以,需要延迟5秒才能启动activity 就不能实现完美屏蔽了。。。
加完android:sharedUserId="android.uid.system" 发现项目运行不了,提示INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES
这是因为需要系统签名才能安装,,,那我们就给他系统签名,
里面讲解了如何系统签名,和需要签名的platform.pk8 platform.x509.pem 文件
链接:https://pan.baidu.com/s/1c3sQyMax8TOUb5qpVwwvQw
提取码:bdil
3.最重要的一步(关键点)
解决频繁点击HOME还是会最小化app的问题
在onPause里加入打开activity代码,双重保险 解决了频繁点击还是会退出的问题!
public static boolean keyHomeDownLoad=true;
@Override protected void onPause() { super.onPause(); if(BaseActivity.keyHomeDownLoad){ Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); try { pendingIntent.send(); } catch (Exception e) { startActivity(intent); } } }
如果觉得软件退出不了,也跳转不了别的页面了,可以加变量 做判断控制keyHomeDownLoad=false就可以了