This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 29 | |
buildToolsVersion "29.0.3" | |
defaultConfig { | |
applicationId "com.example.handlegoogleapierror" | |
minSdkVersion 16 | |
targetSdkVersion 29 | |
versionCode 1 | |
versionName "1.0" | |
multiDexEnabled true | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation 'androidx.appcompat:appcompat:1.2.0' | |
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | |
implementation 'com.google.android.gms:play-services:12.0.1' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.handlegoogleapierror; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.app.Dialog; | |
import android.content.Intent; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GoogleApiAvailability; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationServices; | |
public class MainActivity extends AppCompatActivity { | |
private final int REQUEST_RESOLVE_GOOGLE_CLIENT_ERROR=1; | |
boolean mResolvingError; | |
GoogleApiClient mGoogleApiClient; | |
GoogleApiClient.ConnectionCallbacks mConnectionCallbacks = | |
new GoogleApiClient.ConnectionCallbacks() { | |
@Override | |
public void onConnected(Bundle bundle) { | |
Toast.makeText(MainActivity.this, "onConnected()", Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
}; | |
GoogleApiClient.OnConnectionFailedListener mOnConnectionFailedListener = | |
new GoogleApiClient.OnConnectionFailedListener() { | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Toast.makeText(MainActivity.this, connectionResult.toString(), Toast.LENGTH_LONG).show(); | |
if (mResolvingError) { | |
return; | |
} else if (connectionResult.hasResolution()) { | |
mResolvingError = true; | |
try { | |
connectionResult.startResolutionForResult(MainActivity.this, | |
REQUEST_RESOLVE_GOOGLE_CLIENT_ERROR); | |
} catch (IntentSender.SendIntentException e) { | |
mGoogleApiClient.connect(); | |
} | |
} else { | |
showGoogleAPIErrorDialog(connectionResult.getErrorCode()); | |
} | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setupGoogleApiClient(); | |
} | |
private void showGoogleAPIErrorDialog(int errorCode) { | |
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); | |
Dialog errorDialog = googleApiAvailability.getErrorDialog( | |
this, errorCode, REQUEST_RESOLVE_GOOGLE_CLIENT_ERROR); | |
errorDialog.show(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_RESOLVE_GOOGLE_CLIENT_ERROR) { | |
mResolvingError = false; | |
if (resultCode == RESULT_OK && !mGoogleApiClient.isConnecting() | |
&& !mGoogleApiClient.isConnected()) { | |
mGoogleApiClient.connect(); | |
} | |
} | |
} | |
protected void setupGoogleApiClient() { | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(mConnectionCallbacks) | |
.addOnConnectionFailedListener(mOnConnectionFailedListener) | |
.addApi(LocationServices.API) | |
.build(); | |
mGoogleApiClient.connect(); | |
} | |
} |
0 Comments