IntentSender, this
IntentSender , , ( ). .
ResultCallback<ContentsResult> onContentsCallback =
new ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType(MIME_TYPE_TEXT).build();
IntentSender createIntentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialContents(result.getContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(createIntentSender, REQUEST_CODE_CREATOR, null,
0, 0, 0);
} catch (SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
};
`startIntentSenderForResult (IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)`
requestCode >= 0, onActivityResult(), .
onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_CREATOR:
if (resultCode == RESULT_OK) {
DriveId driveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
showMessage("File created with ID: " + driveId);
}
finish();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
apiClient,
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API).addScope(Drive.SCOPE_FILE)
.setAccountName(mAccountName).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "GoogleApiClient connected");
}
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
mAccountName :
Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
if (accounts.length == 0) {
Log.d(TAG, "Must have a Google account installed");
return;
}
mAccountName = accounts[0].name;
, .