Fix crash on application startup if the Google APIs are not available.

The Google APIs are not available on x86 emulator and so the application
was crashing.
This commit is contained in:
Ghislain MARY 2012-11-16 14:05:21 +01:00
parent 9885d993b3
commit 549695216c

View file

@ -42,25 +42,29 @@ public class ApiEightPlus {
public static void initPushNotificationService(Context context) { public static void initPushNotificationService(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Starting the push notification service try {
GCMRegistrar.checkDevice(context); // Starting the push notification service
GCMRegistrar.checkManifest(context); GCMRegistrar.checkDevice(context);
final String regId = GCMRegistrar.getRegistrationId(context); GCMRegistrar.checkManifest(context);
String newPushSenderID = context.getString(R.string.push_sender_id); final String regId = GCMRegistrar.getRegistrationId(context);
String currentPushSenderID = prefs.getString(context.getString(R.string.push_sender_id_key), null); String newPushSenderID = context.getString(R.string.push_sender_id);
if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) { String currentPushSenderID = prefs.getString(context.getString(R.string.push_sender_id_key), null);
GCMRegistrar.register(context, newPushSenderID); if (regId.equals("") || currentPushSenderID == null || !currentPushSenderID.equals(newPushSenderID)) {
GCMRegistrar.register(context, newPushSenderID);
Log.d("Push Notification : storing current sender id = " + newPushSenderID); Log.d("Push Notification : storing current sender id = " + newPushSenderID);
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putString(context.getString(R.string.push_sender_id_key), newPushSenderID); editor.putString(context.getString(R.string.push_sender_id_key), newPushSenderID);
editor.commit(); editor.commit();
} else { } else {
Log.d("Push Notification : already registered with id = " + regId); Log.d("Push Notification : already registered with id = " + regId);
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putString(context.getString(R.string.push_reg_id_key), regId); editor.putString(context.getString(R.string.push_reg_id_key), regId);
editor.commit(); editor.commit();
}
} catch (java.lang.UnsupportedOperationException e) {
Log.i("Push Notification not activated");
} }
} }
} }