java.lang.SecurityException:Permission denial:写入设置需要android.permission.WRITE_SETTINGS

时间:2015-12-22 09:15:10

标签: java android

这是我在平台5.0.0上构建Airplan模式On / OFF的代码 下面是我的Activity.java源代码,请在manifestast.xml中建议我需要做的更改才能访问权限。

package com.rjil.airplanemodeonoff;

import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    // constants
    static final String STATUS_ON = "Airplane Mode: ON";
    static final String STATUS_OFF = "Airplane Mode: OFF";

    static final String TURN_ON = "Turn ON";
    static final String TURN_OFF = "Turn OFF";

    // controls
    TextView TVAirplaneMode;
    ToggleButton tBAirplane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // load controls
        TVAirplaneMode = (TextView)findViewById(R.id.TVAirplaneMode);
        tBAirplane = (ToggleButton)findViewById(R.id.tBAirplane);
        // update UI at first time loading
        updateUI(isAirplaneMode());
        // set click event for button
        tBAirplane.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // check current state first
                boolean state = isAirplaneMode();
                // toggle the state
                if (state) toggleAirplaneMode(0, state);
                else toggleAirplaneMode(1, state);
                // update UI to new state
                updateUI(!state);
            }
        });
    }
    public void toggleAirplaneMode(int value, boolean state) {
        // toggle airplane mode
        //check the version
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //if less than verson 4.2
            Settings.System.putInt(
                    getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, value);
        } else {
            Settings.Global.putInt(
                    getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, value);
        }
        // broadcast an intent to inform
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !state);
        sendBroadcast(intent);
    }

    public void updateUI(boolean state) {
        //set text according to state
        if(state) {
            TVAirplaneMode.setText(STATUS_ON);
            tBAirplane.setText(TURN_OFF);
        } else {
            TVAirplaneMode.setText(STATUS_OFF);
            tBAirplane.setText(TURN_ON);
        }
    }


    public boolean isAirplaneMode() {
        //check the version
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {//if less than verson 4.2
            return Settings.System.getInt(getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;
        } else {
            return Settings.Global.getInt(getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;

        }
    }



}

1 个答案:

答案 0 :(得分:3)

WRITE_SECURE_SETTINGS不供第三方应用程序使用。无论您是使用root还是生产版本,系统/固件之外的任何应用都无法获得该权限。

请检查this回答