OK、私はここで何が欠けているのか本当にわかりません。私はC2DMをアプリケーションで機能させようとしていますが、特にブロードキャストの処理には苦労しています。
アプリ全体のBroadcastReceiverがあります。
public final class AppBroadcastReceiver extends BroadcastReceiver {
//get an instance if not already present
public static AppBroadcastReceiver getInstance(final IntentFilter filter) {
if (instance == null) {
instance = new GlobalBroadcastReceiver();
}
if (filter != null) {
filter.addAction("com.google.android.c2dm.intent.REGISTRATION";
filter.addAction("com.google.android.c2dm.intent.RECEIVE");
filter.addCategory("my.package.name");
}
return instance;
}
@Override
public void onReceive(Context context, Intent intent) {
final String broadcastAction = intent.getAction();
Log.d(logTag, String.format("GlobalBroadcastReceiver::onReceive for action = %s", broadcastAction));
if ("com.google.android.c2dm.intent.REGISTRATION".equals(broadcastAction)) {
for (final AppBroadcastListener l : listeners) {
l.c2dmRegistration(intent);
}
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(broadcastAction)) {
for (final ApplBroadcastListener l : listeners) {
l.c2dmReceive(intent);
}
}//else
}//onReceive
}
AppBroadcastListenerは、適切なメソッドが少なくとも存在することを保証するために、すべてのアクティビティが実装しているインターフェイスです。onResume()では、onStop()は、それぞれレシーバーで登録されているアクティビティと登録解除されているアクティビティをメソッド化します。
テストの目的で、次の2つの方法を証明するデバッグアクティビティがあります。
public void sendC2DM(View v){
Intent intent= new Intent();
intent.setAction(com.google.android.c2dm.intent.RECEIVE);
intent.putExtra("message","Bender: \"kiss my shiny metal ass!\"");
intent.addCategory(getPackageName() );
sendBroadcast(intent);
}
public void registerC2DM(View v){
Intent registrationIntent = new Intent(com.google.android.c2dm.intent.REGISTRATION);
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", ourSenderIdregistered@googlemail.com);
startService(registrationIntent);
}
そして、android.manifestで、-tagに次の行を追加しました<application>
。
<receiver
android:name=".AppBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="my.package.name" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.package.name" />
</intent-filter>
</receiver>
したがって、ブロードキャストを受信するすべてのアクティビティは、onResume()のBroadcastReceiverに登録されており、何かが発生すると、BroadcastReceiverは実装されたメソッドをキャッチして呼び出します。たとえば、メッセージを記録したり、トーストを表示したりします。
ただし、「C2DMメッセージ」を送信すると、この構造が自作のブロードキャストで機能していることがわかります。(ベンダーのメッセージがトーストにポップアップ表示されます)しかしregisterC2DM().startService(registrationIntent);
、ログに記録するだけです:
サービスを開始できませんインテント{act=com.google.android.c2dm.intent.REGISTRATION(追加機能あり)}:見つかりません
ここで何が欠けているのかわかりません。一般的なアドバイスは次のように思われます:android.manifestを確認する(完了)または:登録されたGmailアカウントでログインします。
これについてはよくわかりません。私は自分のGmailアカウントでログインしていますがourSenderIdregistered@googlemail.com
、登録時に意図したものではありません。また、これが解決策になることはないと強く信じています。(すべてのお客様にこのアカウントでログインするように伝えます...えーと?!)。
だから私はそれが何か他のものだと思いますが、私はそれを見つけることができません:C