Adding Google Play Services and checking for Google Play Services before attempting GCM implementation

This commit is contained in:
Dan Galpin
2015-05-24 22:05:41 -07:00
parent e99968e442
commit 2ede69cc38
2 changed files with 41 additions and 3 deletions

View File

@@ -24,4 +24,5 @@ dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.android.support:support-annotations:22.0.0'
compile 'com.google.android.gms:play-services-gcm:7.0.0'
}

View File

@@ -19,15 +19,19 @@ import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback {
private final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String DETAILFRAGMENT_TAG = "DFTAG";
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private boolean mTwoPane;
private String mLocation;
@@ -61,6 +65,13 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
forecastFragment.setUseTodayLayout(!mTwoPane);
SunshineSyncAdapter.initializeSyncAdapter(this);
if (!checkPlayServices()) {
// this is where we could either prompt a user that they should install
// the latest version of Google Play Services, or add an error snackbar
// that some features won't be available.
}
}
@Override
@@ -82,13 +93,19 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
// If Google Play Services is not available, some features, such as GCM-powered weather
// alerts, will not be available.
if (!checkPlayServices()) {
// Store regID as null
}
String location = Utility.getPreferredLocation(this);
// update the location in our second pane using the fragment manager
if (location != null && !location.equals(mLocation)) {
@@ -125,4 +142,24 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
startActivity(intent);
}
}
/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(LOG_TAG, "This device is not supported.");
finish();
}
return false;
}
return true;
}
}