I have this code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BackupActivity.REQUEST_AUTHORIZE) { ... etc
and when authorizing the user to Google Drive, showing the available accounts, I am using startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_AUTHORIZE, null, 0, 0, 0, null);
according to Google documentation.
However this is deprecated. Is there a way to replace it in my code context? Maybe with ActivityResultCallback or any other working solution?
Identity.getAuthorizationClient(this)
.authorize(authorizationRequest)
.addOnSuccessListener(
authorizationResult -> {
if (authorizationResult.hasResolution()) {
// access needs to be granted by the user
PendingIntent pendingIntent = authorizationResult.getPendingIntent();
try {
assert pendingIntent != null;
startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_AUTHORIZE, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
}
} else {
try {
handleSignInInent(authorizationResult);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
})
.addOnFailureListener(e -> Log.i("TTTT", "Failed to authorize", e));
I tried this:
private final ActivityResultLauncher<IntentSenderRequest> authorizationLauncher = registerForActivityResult(
new ActivityResultContracts.StartIntentSenderForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// Authorization was successful
// Handle the result, e.g., call handleSignInIntent
try {
handleSignInInent(Identity.getAuthorizationClient(this).getAuthorizationResultFromIntent(result.getData()));
} catch (IOException | ApiException e) {
throw new RuntimeException(e);
}
} else {
// Authorization failed or was canceled
Log.i("TTTT", "Authorization failed or was canceled");
}
}
);
PendingIntent pendingIntent = authorizationResult.getPendingIntent();
try {
assert pendingIntent != null;
authorizationLauncher.launch(new Intent().setIntentSender(pendingIntent.getIntentSender()));
} catch (IntentSender.SendIntentException e) {
Log.i("TTTT", "Couldn't start Authorization UI: " + e.getLocalizedMessage());
}
But in second part of my code Cannot resolve method ‘setIntentSender
‘ in ‘Intent’