Android 非ROOT手机 apk 获取更改设置开关权限,比如打开usb调试

参考链接:

https://greenify.uservoice.com/knowledgebase/articles/749142-how-to-grant-permissions-required-by-some-features
https://stackoverflow.com/questions/5034160/how-can-i-get-the-dreaded-write-secure-settings-permission-for-my-android-app

Android获取USB调试状态

获取很简单,适用于API 17+

      //检查USB调试状态
    public static boolean checkUSBDebug(){
        if(Settings.Global.getInt( Context.getContentResolver(), Settings.Global.ADB_ENABLED, 0) == 1) {
           return true;
        } else {
           return false;
        }
    }

Android打开USB调试开关

Settings.Global.putInt(Context.getContentResolver(), Settings.Global.ADB_ENABLED, 1);

需要添加权限,但是这权限仅系统应用有权限申请:


这里有个取巧的办法能获取权限,安装apk后:adb shell执行命令:

adb -d shell pm grant pkgname android.permission.WRITE_SECURE_SETTINGS

这样就能实现当USB调试开关关掉之后,apk自动判断并且去打开了

Android打开开发者选项开关

Settings.Global.putInt(Context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);

Android获取手机是否静音,如果否,则设置静音

    //判断手机是否处于静音状态,如果非静音则设置静音
    public static void checkAudio(Context context){
        AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

        switch (am.getRingerMode()) {
            case AudioManager.RINGER_MODE_SILENT:
                Log.i(TAG,"Silent mode");
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                Log.i(TAG,"Vibrate mode");
                am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
            case AudioManager.RINGER_MODE_NORMAL:
                Log.i(TAG,"Normal mode");
                am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
        }
    }

Android获取手机wifi状态,如果关闭,则打开

    //判断手机wifi是否掉线,如果掉线则强制重连
    public static boolean  checkWifiState(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        //打开wifi
        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
        return false;
    }

需要申请权限:

    
    
    

你可能感兴趣的:(Android 非ROOT手机 apk 获取更改设置开关权限,比如打开usb调试)