Created
July 7, 2016 22:19
-
-
Save damien5314/c13ce47bca035c517dfbdfb2af488a73 to your computer and use it in GitHub Desktop.
Handles errors from GoogleApiClient
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.sample; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.Status; | |
public class ResolveErrorActivity extends AppCompatActivity { | |
public static final String TAG = ResolveErrorActivity.class.getSimpleName(); | |
private static final String EXTRA_STATUS = "EXTRA_STATUS"; | |
private static final String EXTRA_CONNECTION_RESULT = "EXTRA_CONNECTION_RESULT"; | |
public static final int REQUEST_RESOLVE_ERROR = 1001; | |
public static Intent buildIntent(Context context, Status status) { | |
Intent intent = new Intent(context, ResolveErrorActivity.class); | |
intent.putExtra(EXTRA_STATUS, status); | |
return intent; | |
} | |
public static Intent buildIntent(Context context, ConnectionResult result) { | |
Intent intent = new Intent(context, ResolveErrorActivity.class); | |
intent.putExtra(EXTRA_CONNECTION_RESULT, result); | |
return intent; | |
} | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setTitle(null); | |
} | |
private Status getStatus() { | |
return getIntent().getExtras().getParcelable(EXTRA_STATUS); | |
} | |
private ConnectionResult getConnectionResult() { | |
return getIntent().getExtras().getParcelable(EXTRA_CONNECTION_RESULT); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
resolveErrors(); | |
} | |
private void resolveErrors() { | |
try { | |
if (getStatus() != null) { | |
getStatus().startResolutionForResult(this, REQUEST_RESOLVE_ERROR); | |
} else if (getConnectionResult() != null) { | |
getConnectionResult().startResolutionForResult(this, REQUEST_RESOLVE_ERROR); | |
} | |
} catch (IntentSender.SendIntentException e) { | |
Log.d(TAG, "Error occurred while sending Intent", e); | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
setResult(resultCode, data); | |
finish(); | |
} | |
} | |
/** Transparent Activity theme, apply this in AndroidManifest.xml | |
<style name="Transparent" parent="@style/Theme.AppCompat"> | |
<item name="android:windowIsTranslucent">true</item> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
<item name="android:windowContentOverlay">@null</item> | |
<item name="android:windowNoTitle">true</item> | |
<item name="android:windowIsFloating">true</item> | |
<item name="android:backgroundDimEnabled">false</item> | |
</style> | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment