Android 禁用自动更改方向

Android 禁用自动更改方向,android,Android,下面是我的代码。 我想将其设置为禁用自动更改屏幕方向。 并启用自动更改屏幕方向 class NewLoad extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); progDailog = new ProgressDialog(this);

下面是我的代码。
我想将其设置为禁用自动更改屏幕方向。
并启用自动更改屏幕方向

class NewLoad extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progDailog = new ProgressDialog(this);
            progDailog.setMessage("test");
            progDailog.setIndeterminate(false);
            progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDailog.setCancelable(false);
            progDailog.show();
            //set disable auto change
        }

        @Override
        protected String doInBackground(String... aurl) {
            //do something
            return null;
        }

        @Override
        protected void onPostExecute(String unused) {
            super.onPostExecute(unused);
            progDailog.dismiss();
            //set enable auto change
        }
    }
类NewLoad扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progDailog=新建进度对话框(此对话框);
progDailog.setMessage(“测试”);
progDailog.setUndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_微调器);
progDailog.setCancelable(假);
progDailog.show();
//设置禁用自动更改
}
@凌驾
受保护的字符串背景(字符串…aurl){
//做点什么
返回null;
}
@凌驾
受保护的void onPostExecute(字符串未使用){
super.onPostExecute(未使用);
progDailog.disclose();
//设置启用自动更改
}
}
我怎么做

disable auto change screen orientation.And enable auto change screen orientation.
如果要在运行时执行

尝试:

  • 根据需要保留布尔标志,如中所示
  • 覆盖已更改的OnConfiguration,如果已更改;s true将调用传递给super.onConfiguration更改如果false将不传递调用给super

  • 使您的活动在清单中看起来像这样

      <activity
                    android:name=".Hello"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"   <-- Screen will be forced to have portrait
                    android:configChanges="orientation|keyboardHidden|screensize" > <-- No Restart in these cases
    
    运行时中的

    当您要阻止传感器时:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    
    当您要激活时:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    

    你想干什么?如果要禁用自动屏幕旋转。您希望您的应用程序不旋转,还是希望更改系统设置(就像某些小部件/设置应用程序可能希望做的那样)?我希望在onPreExecute中禁用旋转。如果我没有指定android:screenOrientation=“肖像”,它将在htc v one(4.0)中重新启动。我不想确定活动的方向。那么,在旋转设备时,是否有任何方法可以停止重新启动活动而不固定方向?将重新创建设备活动以支持横向模式。您可以做的是将重物保存在某个位置,并在旋转时恢复它们,而不是再次创建。好的,这意味着我必须在保存状态上使用两种黄金方法和onRestoreInstance状态,对吗?如果您只关心旋转,那么就使用GetLastNonfigurationInstance(),但现在它已被弃用。但是如果我指定android:configChanges=“orientation | keyboardHidden”则不会重新启动Motorola milestone(api级别8)和samsung nexus(api级别10)中的活动即使我没有指定android:screenOrientation=“纵向”,它也会在更高版本中重新启动,比如htc v one