等待来自intent用户的结果

时间:2017-04-25 09:24:26

标签: android

我的代码:

public void run() {
    while (!done){
        try {
            this.mListMessagesResponse = service.users().messages().list(userId).setQ("to:" + recipient).execute().getMessages();
            done = true;
        } catch(UserRecoverableAuthIOException e) {
            mActivity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);

            //wait result
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我想等待我的用户的结果再次启动我的循环。 我试着等待,但它没有用。

1 个答案:

答案 0 :(得分:2)

您不需要while循环来获取Activity的结果。已经有一个内置功能来做到这一点。这是onActivityResult

来自文档:

开始活动

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

使用onActivityResult获取结果。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

请点击此处查看更多Getting a Result from an Activity