3

我正在从 googledrive 示例转换为可以更轻松地处理它的类。
在这个例子中,它调用了一个返回 Intent 的方法(这个 Intent 用于选择谷歌帐户)。
主要活动调用 startActivityForResult(intent)。并检查 onActivityResult 上的会话和登录。
谷歌驱动器示例

public class GoogleActivity extends Activity {
    static final int REQUEST_ACCOUNT_PICKER = 1;
    static final int REQUEST_AUTHORIZATION = 2;
    static final int CAPTURE_IMAGE = 3;

    private static Uri fileUri;
    private static Drive service;
    private GoogleAccountCredential credential;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
       //Start account picker here (the intent I've told above)
        startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
      //credential.newChooseAccountIntent() return an intent that can choose google account
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
        case REQUEST_ACCOUNT_PICKER:
                    //Process it when intent exit
            if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
                String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null) {
                    credential.setSelectedAccountName(accountName);
                    service = getDriveService(credential);
                    startCameraIntent();
                }
            }
            break;
        case REQUEST_AUTHORIZATION:
            if (resultCode == Activity.RESULT_OK) {
                saveFileToDrive();
            } else {
                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
            }
            break;
        case CAPTURE_IMAGE:
            if (resultCode == Activity.RESULT_OK) {
                saveFileToDrive();
            }
        }
    }

    private void startCameraIntent() {
        String mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_" + timeStamp + ".jpg"));

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(cameraIntent, CAPTURE_IMAGE);
    }

    private void saveFileToDrive() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // File's binary content
                    java.io.File fileContent = new java.io.File(fileUri.getPath());
                    FileContent mediaContent = new FileContent("image/jpeg", fileContent);

                    // File's metadata.
                    File body = new File();
                    body.setTitle(fileContent.getName());
                    body.setMimeType("image/jpeg");

                    File file = service.files().insert(body, mediaContent).execute();
                    if (file != null) {
                        showToast("Photo uploaded: " + file.getTitle());
                        startCameraIntent();
                    }
                } catch (UserRecoverableAuthIOException e) {
                    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

    private Drive getDriveService(GoogleAccountCredential credential) {
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
    }

    public void showToast(final String toast) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

现在我想创建一个类来处理 googledrive 的一些功能,包括登录。我的 Login() 方法处理有关登录的所有内容。
我可以在主要活动之外调用 Account Picker 意图,但当该意图退出/销毁时我无法处理。
我的课是这样的:

public class GoogleHandler {
    Activity act;

    public GoogleHandler(Activity act) {
        this.act = act;
    }

    public void Login() {
            //start 
        act.startActivity(intent, requestCode);
    }
    // And then, how to handle when intent exit ??
}

任何建议也对我有所帮助。

4

1 回答 1

1

您传递给的 IntentstartActivity()永远不会开始或结束。这不是 Intents 所做的;Intent 启动另一个 Activity(或 Service),然后启动并结束。

当 Google Account Picker Activity 完成时,onActivityResult()调用 Activity 的方法会被调用。在您的示例中,act接收对onActivityResult(). 您可以在您发布的 Google Drive 示例代码中看到如何正确使用结果的示例。

为了正确接收此回调,actActivity 必须覆盖onActivityResult(). 除非您更改为 Activity,否则您无法在Login()方法或类中执行此操作。GoogleHandlerGoogleHandler

于 2013-07-23T15:13:22.837 回答