锁定方向,直到完成的AsyncTask [英] Lock orientation until Asynctask finish

查看:156
本文介绍了锁定方向,直到完成的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想,直到所有的数据都在我看来装载阻止方向。于是我想到了以下code,但它不能正常工作。

 私有类任务扩展的AsyncTask<弦乐,太虚,字符串> {   保护字符串doInBackground(字符串... PARAMS){
      ...
   }   保护无效onPostExecute(字符串结果){
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   }   在preExecute保护无效(){
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }
}

当我运行传感器的这两行 NOSENSOR 我的屏幕变成横向自动,不理解为什么。这可能是这样吗?

编辑:
我把下面的行来检查,结果如下当前的方向:

 上preExecute保护无效(){
        如果(getResources()。getConfiguration()。方向== Configuration.ORIENTATION_LANDSCAPE){
            Log.e(TAG,横向);
        }其他{
            Log.e(TAG,纵向);
        }
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }

logcat的结果:

 风景

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

 肖像


解决方案

只需更换 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); setRequestedOrientation(ActivityInfo。 SCREEN_ORIENTATION_UNSPECIFIED);

和让我知道发生什么事。

入门级

 上preExecute保护无效(){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}

在退出级别

 保护无效onPostExecute(字符串结果){
      ...
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
   }

更新:

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

  INT currentOrientation = getResources()getConfiguration()方向;

现在设置这个方向在上preExecute() ..

一样,

 上preExecute保护无效(){
  ...
  INT currentOrientation = getResources()getConfiguration()方向。;
   如果(currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
   }
   其他{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
   }
}

简单..: - )

Only want to block orientation until all the data is loaded in my view. So I thought about the following code but it doesn't work.

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);
   }
}

When I run these two lines of SENSOR and NOSENSOR my screen turns horizontal automatically, without understanding why. That may be happening?

EDIT: I put the following lines to check the current orientation with the following result:

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

Result of Logcat:

LANDSCAPE

But if I remove the two lines (SetRequestedOrientation) do I get this in logcat:

PORTRAIT

解决方案

Just replace setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); with setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

And let me know what happen..

For Entry Level

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

At Exit Level

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

Update:

Wired behavior (In your case) but, any way you can get the current orientation using,

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

Now set this orientation in onPreExecute() ..

Like,

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);
   }
}

Simple.. :-)

这篇关于锁定方向,直到完成的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆