调用onPostExecute时完成活动(在AsyncTask中)

时间:2015-07-10 09:53:04

标签: android-asynctask drive

我使用GMS Drive示例演示。

我想选择一个文件(使用“驱动器”对话框打开文件),下载文件,然后完成活动。

我的问题是如果我在onActivityResult中使用finish(),我无法在主活动中得到结果,如果我在onPostExecute中使用finish(),对话框没有关闭,我需要按&#34 ;取消"返回我的主要活动(结果)。我想在没有按下的情况下返回"取消"按钮...

我使用演示中的RetrieveContentsActivity和PickFileWithOpenerActivity。

这是我的代码:

public class RestoreActivity extends DriveActivity {

private static final int REQUEST_CODE_OPENER = 1;

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .setSelectionFilter(Filters.contains(SearchableField.TITLE, "settings"))
            .build(getGoogleApiClient());
    try {
        startIntentSenderForResult(intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        Log.w(TAG, "Unable to send intent", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_OPENER:
            if (resultCode == RESULT_OK) {
                DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                new RetrieveDriveFileContentsAsyncTask(RestoreActivity.this).execute(driveId);
            }
            finish();   // if I put finish() here, I cannot get the result in onActivityResult (main activity)
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

final private class RetrieveDriveFileContentsAsyncTask extends ApiClientAsyncTask<DriveId, Boolean, String> {

    public RetrieveDriveFileContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected String doInBackgroundConnected(DriveId... params) {
        String contents = null;
        DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), params[0]);
        DriveApi.DriveContentsResult driveContentsResult = file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return null;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(driveContents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            contents = builder.toString();
        } catch (IOException e) {
            Log.e(TAG, "IOException while reading from the stream " + e.toString());
        }

        driveContents.discard(getGoogleApiClient());
        return contents;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Intent returnIntent = new Intent();
        returnIntent.putExtra("settings",result);
        if (result == null) {
            Log.w(TAG, "Error");
            setResult(RESULT_CANCELED,returnIntent);
        } else {
            Log.i(TAG, "OK");
            setResult(RESULT_OK,returnIntent);
        }
        // if I put finish() here nothing happens, and dialog is still opened till I press "Cancel" button
    }
}

}

如何在onPostExecute之后返回主活动并停止意图DriveApi?

由于

1 个答案:

答案 0 :(得分:0)

我找到了一个我不喜欢的解决方案:分两步完成。

  1. 使用演示中的PickFileActivity选择文件,通过在MainActivity中宣布将其公开(公共静态DriveId driveId )并更改如下代码来返回driveId:

    MainActivity.driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
    
  2. 在我的MainActivity中移动AsyncTask。

  3. 如果有人找到另一种解决方案?

相关问题