Android 工作室危险权限
Android studio Dangerous permission
我正在尝试通过我的应用程序更改 system/phone 亮度。为此,我需要征得许可 ("dangerous permission")。我不确定我是否采用了正确的方法,但这似乎并不奏效,因为它总是 returns 权限被拒绝。
public void switches() {
if (systemBrightness) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_SETTINGS},
1);
Toast.makeText(this, "Changed to system brightness: changing the “system brightness” is permanent", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Changed to screen brightness: ", Toast.LENGTH_SHORT).show();
}
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
//马尼节
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public void changeScreenBrightness(float brightness) {
mBrightness = brightness;
if (systemBrightness) {
if (!Settings.System.canWrite(this)) {
Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(i);
} else {
Settings.System.putInt(mContentResolver, Settings.System.SCREEN_BRIGHTNESS, (int) (mBrightness * 255)* preset);
}
} else {
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.screenBrightness = mBrightness * preset;
mWindow.setAttributes(mLayoutParams);
}
}
请求危险权限的处理方式不同于易受攻击的权限。您需要使用 Settings.System.canWrite(Context)
方法来检查您是否可以这样做。如果没有通过调用 startActivityForResult
:
向系统发送适当的 Intent
public boolean checkWriteSettingsPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (!Settings.System.canWrite(getContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
} else {
return true;
}
}
中写的所有内容
我正在尝试通过我的应用程序更改 system/phone 亮度。为此,我需要征得许可 ("dangerous permission")。我不确定我是否采用了正确的方法,但这似乎并不奏效,因为它总是 returns 权限被拒绝。
public void switches() {
if (systemBrightness) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_SETTINGS},
1);
Toast.makeText(this, "Changed to system brightness: changing the “system brightness” is permanent", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Changed to screen brightness: ", Toast.LENGTH_SHORT).show();
}
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
//马尼节
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public void changeScreenBrightness(float brightness) {
mBrightness = brightness;
if (systemBrightness) {
if (!Settings.System.canWrite(this)) {
Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(i);
} else {
Settings.System.putInt(mContentResolver, Settings.System.SCREEN_BRIGHTNESS, (int) (mBrightness * 255)* preset);
}
} else {
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.screenBrightness = mBrightness * preset;
mWindow.setAttributes(mLayoutParams);
}
}
请求危险权限的处理方式不同于易受攻击的权限。您需要使用 Settings.System.canWrite(Context)
方法来检查您是否可以这样做。如果没有通过调用 startActivityForResult
:
Intent
public boolean checkWriteSettingsPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (!Settings.System.canWrite(getContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
} else {
return true;
}
}
中写的所有内容