异步任务无法正常工作

async task does not work properly

你好,我有一个从网站数据库获取用户的功能 我的功能

private void get_users() {

    try {
        url = "my address";

        dbGetData3 = new DbGetData();
        new Thread(new Runnable() {
            public void run() {
                data = dbGetData3.getDataFromDB(url);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        userha = parseJSON3(data);
                    }
                });
            }
        }).start();

        Toast.makeText(context, "please wait ", Toast.LENGTH_LONG)
                .show();
    } catch (Exception e) {
        toast(9);
    }

现在我想在获取数据完成时添加一个加载进度条。

我这样使用 AsyncTask:

private class LongOperation extends AsyncTask<String, Void, String> {
    protected void onPreExecute() {

          progressDialog = new ProgressDialog(Login.this);
          progressDialog.setTitle("Processing...");
          progressDialog.setMessage("Please wait...");
          progressDialog.setCancelable(true); 
          progressDialog.show();

    }

    protected String doInBackground(String... params) {
        try {
            get_users();
        } catch (Exception e) {
        }
        return null;
    }

    protected void onPostExecute(String result) {
        progressDialog.dismiss();
    }
}

我用这段代码来执行

        mytask = new LongOperation();
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
            mytask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        else
            mytask.execute();
        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                onCreate(savedInstanceState);
            }
        });

但我没有显示进度对话框(让用户工作)

我这样更改我的代码:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
                mytask.onPreExecute();
                mytask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

            }
            else
            {
                mytask.onPreExecute();
                mytask.execute();
            }

然后我的进度对话框总是显示

我在 Whosebug 中测试其他代码,例如

AsyncTask doInBackground does not run

AsyncTask called from Handler will not execute doInBackground

Android SDK AsyncTask doInBackground not running (subclass)

但这对我不起作用 请帮帮我 tankyou

Consdier 使用 LoaderManager and an AsyncTaskLoader 处理此类内容。

AsyncTasks 令人头疼,因为您必须通过屏幕旋转等方式管理它们的生命周期。有了 LoaderManager,所有这些都成为过去。

下面是加载列表 "items" 的加载器示例。

public class ItemsLoader extends AsyncTaskLoader<List<Item>> {

private static final String TAG = "ItemsLoader";

private List<Item> mItems;
private ItemUpdatedReceiver mObserver;
private int mSomeParam;

public static class ItemUpdatedReceiver extends BroadcastReceiver {

    private static final String TAG = "ItemLoader";

    final ItemsLoader mLoader;

    public ItemUpdatedReceiver(ItemsLoader mLoader) {
        this.mLoader = mLoader;

        // listen for changes to the account we're using
        IntentFilter filter = new IntentFilter(GlobalConstants.ACTION_ITEMS_UPDATED);
        LocalBroadcastManager.getInstance(mLoader.getContext()).registerReceiver(this, filter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (GlobalConstants.ACTION_ITEMS_UPDATED.equals(action)) {

            mLoader.onContentChanged();
        }


    }
}

public void setSomeParam(int someParam){
    mSomeParam = someParam;
    onContentChanged();
}

public ItemsLoader(Context context, int someParam) {
    super(context);
    mSomeParam = someParam;
    onContentChanged();
}

@Override
public List<Item> loadInBackground() {

    // do whatever you need to do here

    ArrayList<Item> Items = new ArrayList<>();
    return Items;

}

/**
 * Called when there is new data to deliever to the client.
 *
 * @param data
 */
@Override
public void deliverResult(List<Item> data) {
    if (isReset()) {
        // an async query came in while the loader is stopped, we don't need the result
        //release resources if needed
        onReleaseResources(data);
    }

    List<Item> oldItems = mItems;
    mItems = data;

    if (isStarted()) {
        // If the Loader is currently started, we can immediately
        // deliver its results.
        super.deliverResult(mItems);
    }

    // At this point we can release the resources associated with
    // 'oldApps' if needed; now that the new result is delivered we
    // know that it is no longer in use.
    if (oldItems != null) {
        onReleaseResources(oldItems);
    }

}

@Override
protected void onStartLoading() {
    super.onStartLoading();
    if (mItems != null) {
        // If we currently have a result available, deliver it
        // immediately.
        deliverResult(mItems);
    }

    // start listening for changes
    if (mObserver == null) {
        mObserver = new ItemUpdatedReceiver(this);
    }

    if (takeContentChanged() || mItems == null) {
        // If the data has changed since the last time it was loaded
        // or is not currently available, start a load.
        forceLoad();
    }
}

/**
 * Handles a request to stop the Loader.
 */
@Override
protected void onStopLoading() {
    // Attempt to cancel the current load task if possible.
    cancelLoad();
}

/**
 * Handles a request to cancel a load.
 */
@Override
public void onCanceled(List<Item> items) {
    super.onCanceled(items);

    // At this point we can release the resources associated with 'profile'
    // if needed.
    onReleaseResources(items);
}

@Override
protected void onReset() {
    super.onReset();

    // Ensure the laoder is stopped
    onStopLoading();

// At this point we can release the resources if needed.
    if (mItems != null) {
        onReleaseResources(mItems);
        mItems = null;
    }

    // Stop monitoring for changes.
    if (mObserver != null) {
        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mObserver);
        mObserver = null;
    }
}

/**
 * Helper function to take care of releasing resources associated
 * with an actively loaded data set.
 */
private void onReleaseResources(List<Item> data) {
    // For a simple List<> there is nothing to do.  For something
    // like a Cursor, we would close it here.

}
}

要在您的 activity 中使用此 class,您必须扩展 LoaderManager.LoaderCallbacks> 并覆盖方法:

 public Loader<List<Item>> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created.  This
    // sample only has one Loader, so we don't care about the ID.
    // start the loading dialog here

    return new ItemsLoader(context);
}

public void onLoadFinished(Loader<List<Item>> loader, List<Item>data) {
    // do something with your data, hide the progress dialog
}

public void onLoaderReset(Loader<Cursor> loader) {
    // set the old data to null
}

真正开始加载:

getLoaderManager().initLoader(LOADER_ID, null, this);