android 屏蔽多任务键 返回键 状态栏下拉

package com.abss.apploader;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import java.lang.reflect.Method;

public class BaseActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prohibitDropDown();
        hideNavigationBar();
        ListenRecentAndHome();
        //根据需要修改以下脚本 hideNavigationBar()需要做同步修改
//        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
//            @Override
//            public void onSystemUiVisibilityChange(int visibility) {
//                Log.e("hehe", Integer.toHexString(visibility));
//                if(visibility==View.VISIBLE) {
//                    hideNavigationBar();
//                }
//            }
//        });
    }
    protected void hideBottomUIMenuV2() {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
    protected void hideBottomUIMenu() {
        int flags;
        int curApiVersion = android.os.Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if(curApiVersion >= Build.VERSION_CODES.KITKAT){
            // This work only for android 4.4+
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
            flags = View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        }else{
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        }

        // must be executed in main thread :)
        getWindow().getDecorView().setSystemUiVisibility(flags);
    }
    public void hideNavigationBar() {

        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                |View.SYSTEM_UI_FLAG_FULLSCREEN
                |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;// hide status bar

        if (android.os.Build.VERSION.SDK_INT >= 19) {
            //请自行查询 IMMERSIVE和IMMERSIVE_STICKY的区别
            uiFlags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY ;//|View.SYSTEM_UI_FLAG_IMMERSIVE;//0x00001000; // SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hid
        } else {
            uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

        try {

            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }
    //一下两个方法并非必须 只是在查找过程中 看到的以重解决方案 可以尝试注释之
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        disableStatusBar();
        super.onWindowFocusChanged(hasFocus);
    }

    public void disableStatusBar() {
        try {
            Object service = getSystemService("statusbar");
            Class<?> claz = Class.forName("android.app.StatusBarManager");
            Method expand = claz.getMethod("collapsePanels");
            expand.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //禁止下拉
    private void prohibitDropDown() {
        manager = ((WindowManager)getSystemService(Context.WINDOW_SERVICE));
        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        localLayoutParams.gravity = Gravity.TOP;
        localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                // this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;
        view = new LinearLayout(this);
        view.setBackgroundColor(0);
        manager.addView(view, localLayoutParams);
    }
    LinearLayout view;
    WindowManager manager;

    @Override
    protected void onDestroy() {

        super.onDestroy();
        manager.removeView(view);

    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Log.d("DeviceKeyMonitor", "按了返回键");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onHomeClick() {
        //Toast.makeText(this,"点击了Home键",Toast.LENGTH_LONG).show();
        Log.d("DeviceKeyMonitor", "按了Home键");
    }

    public void onRecentClick() {
        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.moveTaskToFront(getTaskId(), 0);
    }
    private BroadcastReceiver recentReceiver;
    private static final String SYSTEM_REASON = "reason";
    private static final String SYSTEM_HOME_KEY = "homekey";
    private static final String SYSTEM_HOME_RECENT_APPS = "recentapps";
    public void ListenRecentAndHome(){

        recentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null && intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
                    String reason = intent.getStringExtra(SYSTEM_REASON);
                    if (!TextUtils.isEmpty(reason)){
                        if (SYSTEM_HOME_KEY.equals(reason)){
                            onHomeClick();
                        } else if (SYSTEM_HOME_RECENT_APPS.equals(reason)){
                            onRecentClick();
                        }
                    }
                }
            }
        };
        this.registerReceiver(recentReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    }

    @Override
    protected void onStart() {
        super.onStart();
        this.registerReceiver(recentReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.unregisterReceiver(recentReceiver);

    }
    //打开默认主页面设置Activity
    public void gotoSetDefaultHomePage(View view){
        Intent intent=new Intent(Settings.ACTION_HOME_SETTINGS);
        startActivity(intent);
    }
    public void gotoSetDisplay(View view) {
        Intent intent=new Intent(Settings.ACTION_DISPLAY_SETTINGS);
        startActivity(intent);
    }
    public void gotoSetNetWork(View view) {
        Intent intent=new Intent(Settings.ACTION_WIFI_SETTINGS);
        startActivity(intent);
    }
    public void gotoSetApplicationSettings(View view) {
        Intent intent=new Intent(Settings.ACTION_APPLICATION_SETTINGS);
        startActivity(intent);
    }

    public void gotoSettings(View view){
        Intent intent=new Intent(Settings.ACTION_SETTINGS);
        startActivity(intent);
    }
    public void gotoSettings(String activityName){
        //下面这句用于测试
        activityName="com.android.settings.WirelessSettings";
        Intent intent = new Intent();
        ComponentName cm = new ComponentName("com.android.settings",activityName);
        intent.setComponent(cm);
        intent.setAction(Intent.ACTION_VIEW);
        startActivity(intent);
    }
//    adb shell am start com.android.settings/com.android.settings.Settings
//    安全
//    adb shell am start com.android.settings/com.android.settings.SecuritySettings
//    手机无线信息
//    adb shell am start com.android.settings/com.android.settings.RadioInfo
//    更多页面
//    com.android.settings.AccessibilitySettings 辅助功能设置
//    com.android.settings.ActivityPicker 选择活动
//    com.android.settings.ApnSettings APN设置
//    com.android.settings.ApplicationSettings 应用程序设置
//    com.android.settings.BandMode 设置GSM/UMTS波段
//    com.android.settings.BatteryInfo 电池信息
//    com.android.settings.DateTimeSettings 日期和坝上旅游网时间设置
//    com.android.settings.DateTimeSettingsSetupWizard 日期和时间设置
//    com.android.settings.DevelopmentSettings 开发者设置
//    com.android.settings.DeviceAdminSettings 设备管理器
//    com.android.settings.DeviceInfoSettings 关于手机
//    com.android.settings.Display 显示——设置显示字体大小及预览
//    com.android.settings.DisplaySettings 显示设置
//    com.android.settings.DockSettings 底座设置
//    com.android.settings.IccLockSettings SIM卡锁定设置
//    com.android.settings.InstalledAppDetails 语言和键盘设置
//    com.android.settings.LanguageSettings 语言和键盘设置
//    com.android.settings.LocalePicker 选择手机语言
//    com.android.settings.LocalePickerInSetupWizard 选择手机语言
//    com.android.settings.ManageApplications 已下载(安装)软件列表
//    com.android.settings.MasterClear 恢复出厂设置
//    com.android.settings.MediaFormat 格式化手机闪存
//    com.android.settings.PhysicalKeyboardSettings 设置键盘
//    com.android.settings.PrivacySettings 隐私设置
//    com.android.settings.ProxySelector 代理设置
//    com.android.settings.RadioInfo 手机信息
//    com.android.settings.RunningServices 正在运行的程序(服务)
//    com.android.settings.SecuritySettings 位置和安全设置
//    com.android.settings.Settings 系统设置
//    com.android.settings.SettingsSafetyLegalActivity 安全信息
//    com.android.settings.SoundSettings 声音设置
//    com.android.settings.TestingSettings 测试——显示手机信息、电池信息、使用情况统计、Wifi information、服务信息
//    com.android.settings.TetherSettings 绑定与便携式热点
//    com.android.settings.TextToSpeechSettings 文字转语音设置
//    com.android.settings.UsageStats 使用情况统计
//    com.android.settings.UserDictionarySettings 用户词典
//    com.android.settings.VoiceInputOutputSettings 语音输入与输出设置
//    com.android.settings.WirelessSettings 无线和网络设置


}

相关权限

    
    <uses-permission android:name="android.permission.REORDER_TASKS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />


    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.STATUS_BAR" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

    <uses-permission android:name = "android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name = "android.permission.WAKE_LOCK"/>

当设置为主页时 需要提供一些特定逻辑 (例如设置密码,连续点击多次或特定手势等)以便管理员可以跳转到特定的设置页面 

如上

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值