下面列出了android.content.IntentSender.SendIntentException#com.google.android.gms.common.api.ApiException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Log.w("TAG", "Google sign in failed", e);
// ...
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(this, "Failed to sign in, Error: "+e.getStatusCode(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
}
}
if (requestCode == 104) {
if (resultCode==1) {
boolean isPaid = data.getBooleanExtra("payment",false);
if (isPaid) {
setSuccess(data.getStringExtra("client"),
FirebaseDatabase.getInstance().getReference("orders"));
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
findViewById(R.id.signInButton).setVisibility(View.GONE);
Toast.makeText(this, "Logged in:"+account.getDisplayName(), Toast.LENGTH_SHORT)
.show();
} catch (ApiException e) {
e.printStackTrace();
Toast.makeText(this, "Sign in failed:"+e.getLocalizedMessage(), Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w("TAG", "Google sign in failed", e);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent in signIn()
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(intent);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
onConnected(account);
} catch (ApiException apiException) {
String message = apiException.getMessage();
if (message == null || message.isEmpty()) {
message = getString(R.string.signin_other_error);
}
onDisconnected();
new AlertDialog.Builder(this)
.setMessage(message)
.setNeutralButton(android.R.string.ok, null)
.show();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==103) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(activity, "Failed to sign in, Error: "+e.getStatusCode(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void onActivityResult(ActivityResult activityResult) {
// The Task returned from getSignedInAccountFromIntent is always completed, so no need to
// attach a listener.
try {
Task<GoogleSignInAccount> googleSignInTask =
GoogleSignIn.getSignedInAccountFromIntent(activityResult.getData());
onGoogleSignIn(googleSignInTask.getResult(ApiException.class));
} catch (ApiException e) {
Log.w(TAG, "Sign in failed: " + e);
signInState.onNext(new SignInState(e));
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
e.printStackTrace();
hideProgressView();
loginError("Google sign in failed.");
}
}
}
@Override
public void onLoginAccountsChanged(Intent data) {
try {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
GoogleSignInAccount account = task.getResult(ApiException.class);
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
signInCurrentAccount(googleSignInAccount);
} catch (ApiException apiException) {
Log.e(TAG, "GoogleSignIn api exception");
}
}
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if(requestCode == CA.AUTH) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
mSignInAccount = task.getResult(ApiException.class);
mSnapshotClient = Games.getSnapshotsClient(activity, mSignInAccount);
mListener.onSyncProgress(CA.AUTH);
read();
} catch (ApiException e) {
mListener.onSyncFailed(CA.CONNECTION);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(intent);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
onConnected(account);
} catch (ApiException apiException) {
String message = apiException.getMessage();
if (message == null || message.isEmpty()) {
message = getString(R.string.signin_other_error);
}
onDisconnected();
new AlertDialog.Builder(this)
.setMessage(message)
.setNeutralButton(android.R.string.ok, null)
.show();
}
} else if (requestCode == RC_LOOK_AT_MATCHES) {
// Returning from the 'Select Match' dialog
if (resultCode != Activity.RESULT_OK) {
logBadActivityResult(requestCode, resultCode,
"User cancelled returning from the 'Select Match' dialog.");
return;
}
TurnBasedMatch match = intent
.getParcelableExtra(Multiplayer.EXTRA_TURN_BASED_MATCH);
if (match != null) {
updateMatch(match);
}
Log.d(TAG, "Match = " + match);
} else if (requestCode == RC_SELECT_PLAYERS) {
// Returning from 'Select players to Invite' dialog
if (resultCode != Activity.RESULT_OK) {
// user canceled
logBadActivityResult(requestCode, resultCode,
"User cancelled returning from 'Select players to Invite' dialog");
return;
}
// get the invitee list
ArrayList<String> invitees = intent
.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// get automatch criteria
Bundle autoMatchCriteria;
int minAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers = intent.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(minAutoMatchPlayers,
maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
.addInvitedPlayers(invitees)
.setAutoMatchCriteria(autoMatchCriteria).build();
// Start the match
mTurnBasedMultiplayerClient.createMatch(tbmc)
.addOnSuccessListener(new OnSuccessListener<TurnBasedMatch>() {
@Override
public void onSuccess(TurnBasedMatch turnBasedMatch) {
onInitiateMatch(turnBasedMatch);
}
})
.addOnFailureListener(createFailureListener("There was a problem creating a match!"));
showSpinner();
}
}
@Override
public void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(GOOGLE_TAG, "Google SignIn activity result.");
try {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
if (account != null) {
Log.d(GOOGLE_TAG, "Google Sign In succeed.");
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
this.plugin.handleAuthCredentials(credential);
return;
}
} catch (ApiException exception) {
// Google Sign In failed, update UI appropriately
Log.w(GOOGLE_TAG, GoogleSignInStatusCodes.getStatusCodeString(exception.getStatusCode()), exception);
plugin.handleFailure("Google Sign In failure.", exception);
return;
}
plugin.handleFailure("Google Sign In failure.", null);
}
private void getPlaceById() {
// [START maps_places_get_place_by_id]
// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";
// Specify the fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Construct a request object, passing the place ID and fields array.
final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
Place place = response.getPlace();
Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
});
// [END maps_places_get_place_by_id]
}
@ReactMethod
public void rejectConnection(final String serviceId, final String endpointId) {
final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);
logV("rejecting connection from " + endpointId);
final ConnectionsClient clientSingleton = getConnectionsClientSingleton(serviceId);
clientSingleton
.rejectConnection(endpointId)
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
ApiException apiException = (ApiException) e;
logW("rejectConnection() failed.", e);
}
});
}
@ReactMethod
public void acceptConnection(final String serviceId, final String endpointId) {
final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);
logV("acceptConnection(serviceId: "+serviceId+", endpointId:" + endpointId+")");
final ConnectionsClient clientSingleton = getConnectionsClientSingleton(serviceId);
clientSingleton
.acceptConnection(endpointId, getPayloadCallback(serviceId))
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
ApiException apiException = (ApiException) e;
logW("acceptConnection(serviceId: "+serviceId+", endpointId:" + endpointId+") failed.", e);
}
});
}
/**
* Sends a {@link Payload} to all currently connected endpoints.
*
* @param payload The data you want to send.
*/
protected void sendPayload(final String serviceId, final String endpointId, final Payload payload) {
final ConnectionsClient clientSingleton = getConnectionsClientSingleton(serviceId);
clientSingleton
.sendPayload(endpointId, payload)
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
ApiException apiException = (ApiException) e;
logW("sendPayload() failed.", e);
onSendPayloadFailed(serviceId, endpointId, payload, apiException.getStatusCode());
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed
Toast.makeText(LoginActivity.this, "Sign in fails: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(context, getString(R.string.auth_failed), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> SignInTask)
{
boolean signInSuccessfully = true;
try
{
loadSignedInAccountInfo(SignInTask.getResult(ApiException.class));
}
catch(ApiException e)
{
switch(e.getStatusCode())
{
case GoogleSignInStatusCodes.DEVELOPER_ERROR:
Log.d(TAG, "DEVELOPER_ERROR -> Have you signed your project on Android console?");
break;
case GoogleSignInStatusCodes.SIGN_IN_REQUIRED:
Log.d(TAG, "SIGN_IN_REQUIRED -> You have to signin by select account before use this call");
break;
}
signInSuccessfully = false;
mGoogleSignInClient = null;
}
signedIn(signInSuccessfully);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent in signIn()
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
@Test
public void onActivityResult_validRequestCode_apiException() throws Throwable {
// Given
FuturePromise promise = Mockito.mock(FuturePromise.class);
GoogleSignInListener listener = new GoogleSignInListener(promise);
Intent intent = PowerMockito.mock(Intent.class);
int requestCode = Const.GOOGLE_SIGN_IN;
int resultCode = -1;
final Task task = Mockito.mock(Task.class);
Mockito.when(task.getResult(ApiException.class)).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
throw new ApiException(Status.RESULT_CANCELED);
}
});
Mockito.when(GoogleSignIn.getSignedInAccountFromIntent(Mockito.any(Intent.class))).thenReturn(task);
// When
listener.onActivityResult(requestCode, resultCode, intent);
// Then
Mockito.verify(((AndroidApplication) Gdx.app), VerificationModeFactory.times(1)).removeAndroidEventListener(Mockito.refEq(listener));
Mockito.verify(promise, VerificationModeFactory.times(1)).doFail(Mockito.nullable(Exception.class));
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
@Override
public void onFailure(@NonNull Exception e) {
// An error occurred while communicating with the service.
mResult = null;
if (e instanceof ApiException) {
// An error with the Google Play Services API contains some additional details.
ApiException apiException = (ApiException) e;
Log.d(TAG, "Error: " +
CommonStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": " +
apiException.getStatusMessage());
} else {
// A different, unknown type of error occurred.
Log.d(TAG, "ERROR! " + e.getMessage());
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GET_AUTH_CODE) {
// [START get_auth_code]
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
String authCode = account.getServerAuthCode();
// Show signed-un UI
updateUI(account);
// TODO(developer): send code to server and exchange for access/refresh/ID tokens
} catch (ApiException e) {
Log.w(TAG, "Sign-in failed", e);
updateUI(null);
}
// [END get_auth_code]
}
}
private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
Log.d(TAG, "handleSignInResult:" + completedTask.isSuccessful());
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
updateUI(account);
// Store the account from the result
mAccount = account.getAccount();
// Asynchronously access the People API for the account
getContacts();
} catch (ApiException e) {
Log.w(TAG, "handleSignInResult:error", e);
// Clear the local account
mAccount = null;
// Signed out, show unauthenticated UI.
updateUI(null);
}
}
@Override
public boolean isAuthenticated() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this.plugin.getContext());
if (account != null) {
String token = account.getIdToken();
if (token == null) {
Log.d(GOOGLE_TAG, "Google account found, but there is no token to check or refresh.");
return false;
} else {
if (new JWT(token).isExpired(10)) {
try {
Task<GoogleSignInAccount> task = this.mGoogleSignInClient.silentSignIn();
account = task.getResult(ApiException.class);
Log.d(GOOGLE_TAG, "Google silentSignIn succeed.");
} catch (ApiException exception) {
Log.w(GOOGLE_TAG, String.format("Google silentSignIn failure: %s", exception.getLocalizedMessage()));
return false;
}
}
}
}
return account != null;
}
private void startLocationUpdates() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(LocationPickerActivity.this, "Location not Available", Toast.LENGTH_SHORT).show();
return;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest,
locationCallback,
null /* Looper */)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "startLocationUpdates: onSuccess: ");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
Log.d(TAG, "startLocationUpdates: " + ((ApiException) e).getMessage());
} else {
Log.d(TAG, "startLocationUpdates: " + e.getMessage());
}
}
});
}
protected void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUi(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG,
"signInResult:failed code=" + e.getStatusCode() + ". Msg=" + GoogleSignInStatusCodes.getStatusCodeString(e.getStatusCode()));
updateUi(null);
}
}