关于禁用系统app栏(返回键,menu键,home键,下拉状态栏)

最近接到一个需求,公司老总要求指定机型(安卓5.1版本)app进去以后无法退出,防止用户玩别的软件。我真的懵逼了。。。。。。(what f**k!)
这与我当时做的一个平板的项目有关,那个平板有系统提供的方法去禁用系统栏。所以老总看到以后,认为手机也是一样的,所以提出这个需求。于是我立马找到合作厂商技术反馈有没有禁用系统栏一项。那边估计也是懵逼一脸,起初还答复,后面干脆视而不见了。好吧自己弄。


禁用返回按键


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
           //监控/拦截/屏蔽返回键
            return false;
        } 
        return super.onKeyDown(keyCode, event);
    }

这个是众所周知

禁用menu按键
自己创建自己的MyApplication ,然后在其中添加方法如下

/**
 * Created by Jiu on 2019/3/1.
 */

public class MyApplication extends Application
{
    public int count = 0;

    @Override
    public void onCreate()
    {
        super.onCreate ();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {
                Log.v("MyApplication", activity + "onActivityStopped");
                count--;
                if (count == 0) {
                    Log.v("MyApplication", "切换到后台");
                    activity.startActivity(new Intent (getApplicationContext (),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //获取ActivityManager
                     ActivityManager activityManager = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE);

                    /**获得当前运行的task(任务)*/
                    List taskInfoList = activityManager.getRunningTasks(100);
                    for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                        /**找到本应用的 task,并将它切换到前台*/
                        if (taskInfo.topActivity.getPackageName().equals(activity.getPackageName())) {
                            activityManager.moveTaskToFront(taskInfo.id, 0);
                            break;
                        }
                    }
                }
            }

           @Override
            public void onActivityStarted(Activity activity) {
                Log.v("MyApplication", activity + "onActivityStarted");
                if (count == 0) {
                    Log.v("MyApplication", "切换到前台");
                }
                count++;
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                Log.v("MyApplication", activity + "onActivitySaveInstanceState");
            }

            @Override
            public void onActivityResumed(Activity activity) {
                Log.v("MyApplication", activity + "onActivityResumed");
            }

            @Override
            public void onActivityPaused(Activity activity) {
                Log.v("MyApplication", activity + "onActivityPaused");
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                Log.v("MyApplication", activity + "onActivityDestroyed");
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.v("MyApplication", activity + "onActivityCreated");
            }
        });
    }
}

顺便说一句这个方法还有个好处就是下拉状态栏也能一并的处理。。。能打开系统的状态栏,但是点进去以后也能返回来

禁用home键
这个真的一脸的懵逼了 安卓5.1以后的系统没有以前那种直接监控home键的方式了。问了好多个搞这个的也说没方法实现。
百度一下,真的出来一堆以前的方法。我都试过了没啥用。纠结半天了,想一想能不能先实现home键点击监控吧!还真有方法Android之最简单和靠谱的监听Home键和菜单键(最近任务栏)
照着他的改改试试

/**
 * Created by Jiu on 2019/3/1.
 */

public class HomeReceiver extends BroadcastReceiver
{
    static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
    static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
    static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction ();
        //按下Home键会发送ACTION_CLOSE_SYSTEM_DIALOGS的广播
        if (action.equals (Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
        {
            String reason = intent.getStringExtra (SYSTEM_DIALOG_REASON_KEY);
            if (reason != null)
            {
                if (reason.equals (SYSTEM_DIALOG_REASON_HOME_KEY))
                {
                    Log.d ("HomeReceiver", "按了home");
                    Intent intent1 = new Intent (context, MainActivity.class);

                    intent1.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

                    PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, intent1, 0);

                    try
                    {
                        pendingIntent.send ();

                    } catch (PendingIntent.CanceledException e)
                    {
                        e.printStackTrace ();
                    }

                }
            }
        }

    }
}

activity中

        HomeReceiver innerReceiver = new HomeReceiver();//注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(innerReceiver, intentFilter);

AndroidManifest.xml中别忘记注册


试一试。。。点击一次还是可以的点一次恢复一次。我还没来得及高兴。不小心快速多点了几次。。。有点卡顿,一查才发现
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
D/HomeReceiver: 按了home
这尼玛log不停的给我报这个点击事件。。。。整个人都不好了!
可能是我没注销服务,于是立马去改代码在onDestroy()中加上

unregisterReceiver(innerReceiver);

尼玛没有 多少效果。。。还是一样的 单次点击可能有效果,但是快速点击。。。会多次重复接收到服务,造成activity卡主。先这样吧!再继续优化看看

从源码角度去解决。。。。。底层改代码(未果)

你可能感兴趣的:(关于禁用系统app栏(返回键,menu键,home键,下拉状态栏))