gpt4 book ai didi

Android:切换加速度计旋转状态设置

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:48 25 4
gpt4 key购买 nike

我在您的应用程序找到了很多设置旋转的示例,但我遇到的问题是切换位于以下位置的手机设置:设置、显示、自动旋转屏幕(复选框)
我希望能够检查手机的旋转状态,将其设置为相反(自动或关闭),然后关闭应用程序。

这是我得到的:

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

public class Rotation extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

try{
if (android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
android.provider.Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
Toast.makeText(Rotation.this, "Rotation OFF", Toast.LENGTH_SHORT).show();
finish();
}
else{
android.provider.Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show();
finish();
}
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(Rotation.this, "Try failed!", Toast.LENGTH_SHORT).show();
finish();
}
}
}

如果您需要了解更多信息,请告诉我:)
感谢任何帮助,这让我感到压力!!

最佳答案

您是否设置了正确的权限?

我没有,异常详细信息说,

“权限拒绝:从 pid=306 写入 com.android.providers.settings.SettingsProvider uri content://settings/system,uid=10037 需要 android.permission.WRITE_SETTINGS”

总结起来差不多。

关于Android:切换加速度计旋转状态设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10036766/

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