if (HXGCMServiceHelper.isGooglePlayServiceAvailable(this, null))
public class HXGCMServiceHelper {
static final private String DB_NAME = "GCMServiceDB";
static final private String KEY_REGISTER_ID = "registerID";
static final public int REQUEST_CODE_RECOVER_PLAY_SERVICE = 1;
static final public int REQUEST_RESULT_CANCEL = 1;
//region = Checking Version = static public void registerWithCheckingVersion(Activity activity, String senderID, HXGCMServiceListener listener)
{
if (!isGooglePlayServiceAvailable(activity, listener)) return;
register(activity, senderID, listener);
}
/** * Result code will always return 1 -> cancel. Open it when this situation is solved. */// static public void receivedMessageFromOnActivityResult(int requestCode, int resultCode, HXGCMServiceListener listener)// {// switch (requestCode)// {// case REQUEST_CODE_RECOVER_PLAY_SERVICE://// if (resultCode != REQUEST_RESULT_OK) listener.userCancelUpdateService();//// break;// default:// }// }
/** * Will check Google Service is available or not, if not, will present alert. */ static public boolean isGooglePlayServiceAvailable(Activity activity, HXGCMServiceListener listener)
{
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int status = googleAPI.isGooglePlayServicesAvailable(activity);
if (status != ConnectionResult.SUCCESS)
{
if (googleAPI.isUserResolvableError(status)) /// Maybe go update. {
showErrorDialog(activity, status, listener);
}
else {
if (listener != null) listener.isNotSupportGooglePlayService();
}
return false;
}
return true;
}
static private void showErrorDialog(Activity activity, int code, final HXGCMServiceListener listener)
{
GoogleApiAvailability.getInstance().getErrorDialog(activity, code, REQUEST_CODE_RECOVER_PLAY_SERVICE, new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
if (listener != null) listener.userCancelUpdateAlert();
}
}).show();
}
//endregion
static public void register(Context context, String senderID, HXGCMServiceListener listener)
{
if (!hasPlayServices(context)) return;
String registerID = getRegisterID(context);
// Already register. if (registerID != null)
{
listener.registerDone(registerID);
return;
}
registerGCMService(context, senderID, listener);
}
static public boolean hasPlayServices(Context context)
{
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
}
static private void registerGCMService(final Context context, final String senderID, final HXGCMServiceListener listener)
{
new Thread(new Runnable() {
@Override public void run() {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String regid;
try {
regid = gcm.register(senderID);
saveRegisterID(context, regid);
if (listener != null) listener.registerDone(regid);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
static private void saveRegisterID(Context context, String registerID)
{
SharedPreferences db = context.getSharedPreferences(DB_NAME, 0);
db.edit()
.putString(KEY_REGISTER_ID, registerID)
.commit();
}
static private String getRegisterID(Context context)
{
return context.getSharedPreferences(DB_NAME, 0).getString(KEY_REGISTER_ID, null);
}
public interface HXGCMServiceListener
{
public void registerDone(String registerID);
public void isNotSupportGooglePlayService();
public void userCancelUpdateAlert();
/** * Use when update result return from onActivityResult. */// public void userCancelUpdateService(); }
}