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.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:appcompat-v7:21.0.2' compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.android.support:support-annotations:22.0.0' 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.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter; 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 { public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback {
private final String LOG_TAG = MainActivity.class.getSimpleName(); private final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String DETAILFRAGMENT_TAG = "DFTAG"; private static final String DETAILFRAGMENT_TAG = "DFTAG";
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private boolean mTwoPane; private boolean mTwoPane;
private String mLocation; private String mLocation;
@@ -61,6 +65,13 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
forecastFragment.setUseTodayLayout(!mTwoPane); forecastFragment.setUseTodayLayout(!mTwoPane);
SunshineSyncAdapter.initializeSyncAdapter(this); 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 @Override
@@ -82,16 +93,22 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
startActivity(new Intent(this, SettingsActivity.class)); startActivity(new Intent(this, SettingsActivity.class));
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
String location = Utility.getPreferredLocation( this );
// 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 // update the location in our second pane using the fragment manager
if (location != null && !location.equals(mLocation)) { if (location != null && !location.equals(mLocation)) {
ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_forecast); ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_forecast);
if ( null != ff ) { if ( null != ff ) {
ff.onLocationChanged(); ff.onLocationChanged();
@@ -125,4 +142,24 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
startActivity(intent); 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;
}
} }