好吧,我真的不知道我在这里缺少什么。我试图让C2DM为我们的应用工作,特别是处理广播让我很挣扎。C2DM inapp注册:无法启动服务意图
我们有一个appwide广播接收器:
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()方法中,活动分别在Receiver注册和取消注册。
出于测试目的,我有一个调试的活动,证明我以下两种方法:
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", [email protected]);
startService(registrationIntent);
}
而在android.manifest我已经添加在<application>
-tag的follwoing线:
<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>
因此,我们希望接收广播的每个活动都是在BroadcastResceiver onResume()上注册自己,当发生某些事情时,BroadcastReceiver将捕获并调用已实现的方法。例如。 ,记录消息或显示吐司。
但是,当我发送一个“C2DM消息”,我看到这个结构正在为自制广播工作。 (本德的消息在吐司弹出),但registerC2DM().startService(registrationIntent);
只是记录:
无法启动服务意向{ 行为= com.google.android.c2dm.intent.REGISTRATION(有演员)}:未发现
我不知道我在这里错过了什么。一般建议似乎是:检查您的android.manifest(完成)或:使用注册的Gmail帐户登录。
不知道这回合。我用我的Gmail帐户登录是的,但不是与我们在注册时输入的[email protected]
。我也坚信这不可能是解决方案。 (告诉我们所有的客户使用这个帐户登录...... ehm no?!)。
所以我想这是别的东西,但我无法找到它:C