Skip to content

Instantly share code, notes, and snippets.

@jmif
Created September 30, 2011 19:08
Show Gist options
  • Save jmif/1254688 to your computer and use it in GitHub Desktop.
Save jmif/1254688 to your computer and use it in GitHub Desktop.
package com.midwestfleet.receivers;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Simple receiver to start the Google C2DM service registration
* This is encapsulated in a receiver so that we can call it using alarms
* multiple times in case an internet connection is not available.
*
* @author jamifsud
*
*/
public class RegisterC2DMReceiver extends BroadcastReceiver {
public static final String BROADCAST_ACTION = "com.midwestfleet.receivers.RegisterC2DMReceiver.receive";
@Override
public void onReceive(Context c, Intent i) {
String email = null;
// Get the devices midwestfleet.com email address
Account[] accounts = AccountManager.get(c).getAccountsByType("com.google");
for (Account account : accounts) {
if (account.name.endsWith("midwestfleet.com")) {
email = account.name;
}
}
// Fire off the intent
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(c, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", email);
c.startService(registrationIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment