Last active
March 22, 2016 08:41
-
-
Save hungdh0x5e/fe285dbcdf6bc9bd8335 to your computer and use it in GitHub Desktop.
GCM
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.hungdh.gcmdemo; | |
public class MainActivity extends AppCompatActivity { | |
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; | |
private static final String TAG = "MainActivity"; | |
private BroadcastReceiver mRegistrationBroadcastReceiver; | |
private ProgressBar mRegistrationProgressBar; | |
private TextView mInformationTextView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); | |
mRegistrationBroadcastReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
mRegistrationProgressBar.setVisibility(ProgressBar.GONE); | |
SharedPreferences sharedPreferences = | |
PreferenceManager.getDefaultSharedPreferences(context); | |
boolean sentToken = sharedPreferences | |
.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false); | |
if (sentToken) { | |
mInformationTextView.setText(getString(R.string.gcm_send_message)); | |
} else { | |
mInformationTextView.setText(getString(R.string.token_error_message)); | |
} | |
} | |
}; | |
mInformationTextView = (TextView) findViewById(R.id.informationTextView); | |
if (checkPlayServices() ) { | |
// Start IntentService to register this application with GCM. | |
Intent intent = new Intent(this, RegistrationIntentService.class); | |
Bundle bundle = new Bundle(); | |
bundle.putString(NAME, "Huy Hung"); | |
bundle.putString(EMAIL, "hungdh0x5e@gmail.com"); | |
intent.putExtra("bundle", bundle); | |
startService(intent); | |
} | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, | |
new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE)); | |
} | |
@Override | |
protected void onPause() { | |
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); | |
super.onPause(); | |
} | |
/** | |
* 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() { | |
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); | |
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); | |
if (resultCode != ConnectionResult.SUCCESS) { | |
if (apiAvailability.isUserResolvableError(resultCode)) { | |
apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) | |
.show(); | |
} else { | |
Log.i(TAG, "This device is not supported."); | |
finish(); | |
} | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment