2

BroadcastReceiver を使用して IntentService から Activity にメッセージを送信しようとしています。IntentService の私のコードは次のとおりです。

/**
 * Received a message from the Google GCM service.
 */
@Override
protected void onMessage(Context context, Intent intent) {

    Log.e(TAG, "Got a new message from GCM");

    // create the intent
    Intent broadcastIntent = new Intent(BROADCAST_NOTIFICATION);
    intent.putExtra("name", "Josh");
    intent.putExtra("broadcasting", true);
    sendBroadcast(broadcastIntent);
}

Activity クラスでは、これらのメッセージをリッスンするレシーバーを登録します。

private class IncomingTransmissionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // if we are looking at a broadcast notification
        if (intent.getAction().equals(GCMIntentService.BROADCAST_NOTIFICATION)) {

            // are we broadcasting?
            boolean broadcasting = intent.getBooleanExtra("broadcasting", false);

            // get the name of the person who is broadcasting
            String name = intent.getStringExtra("name");

            Log.e(TAG, "Got a message, broadcasting= "+ broadcasting + " name= " + name);
        }
    }
}

ブロードキャストを送信すると、ログに次のように出力されます。

メッセージ、ブロードキャスト = null 名前 = null を受け取りました。

Intent.getExtras().getString("name") および intent.getExtras().getBoolean("broadcasting") でさえ null を返します (intent.getExtras() も null を返します)。

私は何を間違っていますか?インテント エクストラが明らかに設定されているのに null になるのはなぜですか?

4

1 に答える 1

4

あなたがしなければなりません:

    // create the intent
    Intent broadcastIntent = new Intent(BROADCAST_NOTIFICATION);
    broadcastIntent.putExtra("name", "Josh");
    broadcastIntent.putExtra("broadcasting", true);
    sendBroadcast(broadcastIntent);
于 2014-05-30T15:16:57.740 に答える