3

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 つの方法があり、どちらを使用してもよいということでよろしいですか?

4

1 に答える 1

3

デバイスをGCMに登録するには2つの方法があるようです。

あなたの2番目のアプローチである本当の方法しかありません。GCMRegistrar最初のアプローチを実装するために、裏で2番目のアプローチを使用します。

また、応答を処理する意図を開始する方法は2つあると感じています。

実際の方法は1つだけです。それは、2番目のアプローチです。GCMBaseIntentService裏で2番目のアプローチを使用して、最初のアプローチを実装します。これがBroadcastReceiver、GCMによって提供されるアプローチをマニフェストに登録する必要がある理由です。

他のすべてが同じであれば、最初のアプローチをお勧めします。これは、より単純で、さまざまな問題を処理できるためです。ただし、どちらの方法でも機能します。

于 2012-08-26T11:52:52.613 に答える