android.permission.WRITE_SECURE_SETTINGS,系统应用程序中的权限拒绝

时间:2016-12-07 07:24:51

标签: android permission-denied

我知道android.permission.WRITE_SECURE_SETTINGS应该在系统应用程序中调用,所以我将我的应用程序推入/ system / app并安装它,但错误信息显示:

 java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
                                                                     at android.os.Parcel.readException(Parcel.java:1620)
                                                                     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                     at android.database.DatabaseUtils.readExceptionFromParcel

我的设备是nexus 6,我已经扎根了。  我添加了

 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
AndroidManifest.xml中的

这是我的代码:

公共类MainActivity扩展了Activity {

public static final String TAG = "Smart/MainActivity";
Switch drawSwitch;
Switch anmiationSwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawSwitch = (Switch) findViewById(R.id.draw_switch);
    anmiationSwitch = (Switch) findViewById(R.id.animation_switch);

    String drawSettings = Settings.Global.getString(getContentResolver(), Settings.Global.DRAW_SETTING);
    String animationSettings = Settings.Global.getString(getContentResolver(), Settings.Global.ANIMATION_SETTING);

    if (drawSettings != null && drawSettings.equals("0")){
        drawSwitch.setChecked(false);
    }else if (drawSettings != null && drawSettings.equals("1")){
        drawSwitch.setChecked(true);
    }

    if (animationSettings != null && animationSettings.equals("0")){
        drawSwitch.setChecked(false);
    }else if (animationSettings != null &&animationSettings.equals("1")){
        drawSwitch.setChecked(true);
    }

    Log.i(TAG, "---->>onCreate DRAW_SETTING: " + Settings.Global.DRAW_SETTING);
    Log.i(TAG, "---->>onCreate ANIMATION_SETTING: " + Settings.Global.ANIMATION_SETTING);
    Log.i(TAG, "---->>onCreate DRAW_SETTING: " + drawSettings);
    Log.i(TAG, "---->>onCreate ANIMATION_SETTING: " + animationSettings);
    Log.i(TAG, "---->>onCreate device root: " + isRoot());

    //String apkRoot = "chmod 777 " + getPackageCodePath();
    String packageName = getApplication().getBaseContext().getPackageName();
    Log.i(TAG, "---->>getPackageName: " + packageName);
    rootCommand(packageName);

    initListener();

}

private void initListener() {
    drawSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            if(checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                        new String[]{Manifest.permission.WRITE_SECURE_SETTINGS},
                        000);
            }

        }
    });

    anmiationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            //DEFAULT : 0
            //0 : original, show animation
            //1 : hide animation
            int result = isChecked ? 1 : 0;
            Settings.Global.putInt(getContentResolver(), Settings.Global.ANIMATION_SETTING, result);
            Log.i(TAG, "---->>anmiationSwitch status changed, now is : " + Settings.Global.getString(getContentResolver(), Settings.Global.ANIMATION_SETTING));
        }
    });

}


public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 000:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Log.i(TAG, "---->>Permission Granted");
            } else {
                // Permission Denied
                Log.i(TAG, "---->>Permission Denied :" + grantResults[0] );
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions,
                    grantResults);
    }
}


public static boolean rootCommand(String packageName) {

    Process process = null;
    DataOutputStream os = null;

    try {

        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("pm grant "+packageName+" android.permission.WRITE_SECURE_SETTINGS \n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (Exception e) {
        Log.i("*** DEBUG ***", "---->>ROOT REE" + e.getMessage());
        return false;

    } finally {

        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {
        }
    }

    Log.i("*** DEBUG ***", "---->>Root SUCccess ");
    return true;

}



public boolean isRoot() {

    boolean root = false;

    try {
        if ((!new File("/system/bin/su").exists())
                && (!new File("/system/xbin/su").exists())) {
            root = false;
        } else {
            root = true;
        }

    } catch (Exception e) {
    }

    return root;
}

}

0 个答案:

没有答案