GCM でアプリケーションを開発しようとしています。GCM の公式ドキュメントを読んだのですが、これには 2 つの方法があるように感じます。
1. Android端末のGCMへの登録について
デバイスを GCM に登録するには 2 つの方法があるようです。
「http://developer.android.com/guide/google/gcm/gs.html#android-app」と言う
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
一方、「http://developer.android.com/guide/google/gcm/gcm.html#registering」は、
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
// sets the app name in the intent
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", senderID);
startService(registrationIntent);
2. GCMサーバーからのレスポンス取得と、それを処理するサービスの起動について
また、応答を処理するための意図を開始するには2つの方法があるように感じます。
「http://developer.android.com/guide/google/gcm/gs.html#android-app」は、次のように指示します。
「com.google.android.gcm.GCMBaseIntentService のサブクラスを作成」
実装する
onRegistered(コンテキスト コンテキスト、文字列 regId)
onUnRegistered (コンテキスト コンテキスト、文字列 regId)
onMessage(コンテキスト コンテキスト、インテント インテント)
...等。
「http://developer.android.com/guide/google/gcm/gcm.html#registering」と記載されていますが、
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent) {
MyIntentService.runIntentInService(context, intent);
setResult(Activity.RESULT_OK, null, null);
}
}
public class MyIntentService extends IntentService {
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = MyIntentService.class;
static void runIntentInService(Context context, Intent intent) {
synchronized(LOCK) {
if (sWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context, MyIntentService.class.getName());
context.startService(intent);
}
@Override
public final void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(intent);
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
} finally {
synchronized(LOCK) {
sWakeLock.release();
}
}
}
}
上記の両方のプロセスを実行するには 2 つの方法があり、どちらを使用してもよいということでよろしいですか?