Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 – 锁定方向,直到Asynctask完成全部内容,希望文章能够帮你解决android – 锁定方向,直到Asynctask完成所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。