gpt4 book ai didi

java - Android 无法访问更改系统设置权限

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:33 24 4
gpt4 key购买 nike

下面的代码应允许访问(权限/禁用)以更改系统设置,如下所示。但它不起作用——开关无法打开。我的安卓版本是9.怎么了?

主要 Activity :

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean enabled = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !android.provider.Settings.System.canWrite(MainActivity.this)) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Settings.System.putInt(getApplicationContext().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
});
}
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.screenrotation">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

通过添加此权限解决:

<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />

关于java - Android 无法访问更改系统设置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60495944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com