English 中文(简体)
A. 更新A AsyncTask的金融情报组
原标题:Updating the UI in an AsyncTask

Hey, So after looking around It seems the consensus is that by using publishProgress() would be the way to update the UI for longer calculations. I seem to haave implemented this incorrectly though, because when I run this code I get an ANR. Can you help me understand why?

Here is my code: http://pastebin.com/zQNhkDJ9

rel=“nofollow”>http://pastebin.com/HcNZtAsx

最佳回答

您的问题可能是,你的工作职责只是要求出版。 进展。 你们的所有工作都在《Progress》的出版中进行,《Progress》的版面已经开通,因此,你仍然能够以这种方式获得美国国家情报局。 你们需要在InBackground做重的提升,然后,在你进行处理之后,你可以更新情报和安全局。

问题回答

你们需要把阿斯辛卡·塔克作为固定的内产阶级,并拥有一个提及你活动的财产。 然后,你可以直接要求你们的活动,就像你要求点名一样。

你们必须知道的一个问题是,你现在必须小心,明确提及你在任何停电背上的活动,否则你就会发出通告,这将造成记忆泄露。

My pattern is to have another basic Java object, usually another private static class in the Activity which I call "StateHolder" and its my gateway for handling the AsyncTask stuff, such as initializing it and clearing it out (setting to NULL) and cancelling any tasks in progress during rotation changes. In your callback for "onRetainNonConfigurationInstance" you can pass use your StateHolder as your marshalled object.

这里有一些粗略的法典:

private static class GetConnectionsStatusTask extends AsyncTask { private BoothActivity mActivity; private Exception mReason;

public GetConnectionsStatusTask(BoothActivity activity) {
    super();
    mActivity = activity;
}

public void setActivity(BoothActivity activity) {
    mActivity = activity;
}

@Override
protected void onPreExecute() {
    if(mActivity != null) {
        mActivity.startProgressBar("Loading", "Please wait");
    }
}

@Override
protected Connections doInBackground(Void... values) {
    try {
        App app = (App)mActivity.getApplication();
        return(app.getApp().userConnections());
    }catch(Exception e) {
        mReason = e;
        return(null);
    }
}

@Override
protected void onPostExecute(Connections status) {
    super.onPostExecute(status);
    if(mActivity != null) {
        mActivity.completedConnectionsStatusCheck(status, mReason);
    }
}       
 }

private static class StateHolder { private GetConnectionsStatusTask connectionsTask;

public void cancelTasks() {
        if (connectionsTask != null) {
            connectionsTask.setActivity(null);
            connectionsTask.cancel(true);
            connectionsTask = null;
        }
}
    public void setActivityForTasks(BoothActivity activity) {
        if (connectionsTask != null) {
            connectionsTask.setActivity(activity);
        }
}

    public void startConnections(BoothActivity activity) {
        if(mIsConnectionsChecking == false) {
            mIsConnectionsChecking = true;
            connectionsTask = new GetConnectionsStatusTask(activity);
            connectionsTask.execute();              
        }
    }
  }

然后,在Pause 我有这样的法典:

        if(mStateHolder != null) {
            mStateHolder.cancelTasks();
        }

在座各位可以重新接纳你们的国家。

    Object retained = getLastNonConfigurationInstance();
    if(retained != null && retained instanceof StateHolder) {
        mStateHolder = (StateHolder) retained;
        mStateHolder.setActivityForTasks(this);
     } else {
       mStateHolder = new StateHolder();
     }

这也将使贵国重新开始目前的活动,以便你能够重新建立任何“倡议”植被,如进展限制更新等。

采用这种模式,你实际上可以承担同侪任务,处理轮流变化,而进展则不必重新开始。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签