后台线程完成工作后如何向前移动主线程

时间:2019-06-28 17:53:47

标签: android android-asynctask

一旦用户启动应用程序,我就会进行网络通话并获取拓扑。 这是要抓住的地方,首先要获取拓扑,我需要其他应用程序的内容提供者提供的值很少。其他答案,互联网上的博客都说对ContentProvider的查询应该在后台线程中进行;这对我也很有意义。但是,如果我创建后台线程并查询CP,则我的应用程序主线程将在从CP获取值之前开始执行。如何解决这种情况?

到目前为止,我尝试使用AsyncTask并在preExecute()中显示“进度对话框”,并在postExecute()上将其关闭,但对于我的情况也没有用。

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // building URI for content resolver
        //contentURI = new Uri.Builder().build().parse(contentPath);
        progressDialog.setMessage("calling ContentProvider");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);

        if(!SplashActivity.this.isFinishing()) {
            progressDialog.show();
        }
        Log.d(TAG, "calling ContentProvider ");
        Log.d(TAG, "calling @ ===> " + contentURI);
    }

    @Override
    protected Cursor doInBackground(Void... voids) {
        ContentResolver resolver = getContentResolver();
        try{
            Cursor cursor = resolver.query(contentURI, null, null, null, null);
            return cursor;
        }catch (Exception e){
            e.getLocalizedMessage();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Cursor cursor) {
        super.onPostExecute(cursor);

        if (null == cursor) {
            Log.e(TAG, "ContentProvider returned null response.");
        } else if (cursor.getCount() < 1) {
            Log.e(TAG, "ContentProvider returned empty response.");
        } else {
            Log.d(TAG, "ContentProvider returned Data Successfully.");
            mContentProviderData = cursor;
            int rawReturned = cursor.getCount();
            Log.d(TAG, "Number of raws returned into CP: " + String.valueOf(rawReturned));
        }

        progressDialog.dismiss();
    }

0 个答案:

没有答案