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

@ -34,33 +34,37 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
@TargetApi(8) @TargetApi(8)
public class ApiEightPlus { public class ApiEightPlus {
public static int getRotation(Display display) { public static int getRotation(Display display) {
return display.getRotation(); return display.getRotation();
} }
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);
SharedPreferences.Editor editor = prefs.edit(); Log.d("Push Notification : storing current sender id = " + newPushSenderID);
editor.putString(context.getString(R.string.push_sender_id_key), newPushSenderID); SharedPreferences.Editor editor = prefs.edit();
editor.putString(context.getString(R.string.push_sender_id_key), newPushSenderID);
editor.commit();
} else { editor.commit();
Log.d("Push Notification : already registered with id = " + regId); } else {
SharedPreferences.Editor editor = prefs.edit(); Log.d("Push Notification : already registered with id = " + regId);
editor.putString(context.getString(R.string.push_reg_id_key), regId); SharedPreferences.Editor editor = prefs.edit();
editor.commit(); editor.putString(context.getString(R.string.push_reg_id_key), regId);
editor.commit();
}
} catch (java.lang.UnsupportedOperationException e) {
Log.i("Push Notification not activated");
} }
} }
} }