android – 锁定方向,直到Asynctask完成

只想阻止方向,直到我的视图中加载所有数据.所以我想到了以下代码,但它不起作用.
private class task extends AsyncTask<String,Void,String> {

   protected String doInBackground(String... params) {
      ...
   }

   protected void onPostExecute(String result) {
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   }

   protected void onPreExecute() {
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }
}

当我运行这两行SENSOR和NOSENSOR时,我的屏幕自动水平转换,而不理解为什么.可能会发生

编辑:
我把以下几行检查当前方向,结果如下:

protected void onPreExecute() {
        if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
            Log.e("TAG","LANDSCAPE");
        }else{
            Log.e("TAG","PORTRAIT");
        }
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }

Logcat的结果:

LANDSCAPE

但是如果我删除了两行(SetRequestedOrientation),我在logcat中得到这个:

PORTRAIT

解决方法

只是替换setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);与setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

让我知道发生了什么

入门级

protected void onPreExecute() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}

退出

protected void onPostExecute(String result) {
      ...
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
   }

更新:

有线行为(在你的情况下),但是,任何方式可以使用目前的方向,

int currentOrientation = getResources().getConfiguration().orientation;

现在在onPreExecute()中设置此方向..

喜欢,

protected void onPreExecute() {
  ...
  int currentOrientation = getResources().getConfiguration().orientation; 
   if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
   }
   else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
   }
}

简单..–)

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...