gpt4 book ai didi

android - C2DM 应用内注册 : unable to start service Intent

转载 作者:太空狗 更新时间:2023-10-29 12:52:49 25 4
gpt4 key购买 nike

好吧,我真的不知道我在这里缺少什么。我试图让 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 是一个接口(interface),我们所有的 Activity 都在实现,以确保至少存在适当的方法。在它们的 onResume() 和 onStop() 方法中, Activity 分别在接收器处注册和取消注册。

出于测试目的,我有一个调试 Activity 证明了以下两种方法:

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 中,我在 <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>

因此,我们想要接收广播的每个 Activity 都在其 onResume() 上的 BroadcastReceiver 上注册自身,当发生某些事情时,BroadcastReceiver 将捕获并调用已实现的方法。例如。 ,记录消息或显示 Toast。

但是,当我发送“C2DM 消息”时,我看到此结构适用于自制广播。 (Bender 的消息在 Toast 中弹出)但是 registerC2DM().startService(registrationIntent);只是记录:

Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not found

我不知道我在这里错过了什么。一般建议似乎是:检查您的 android.manifest(完成)或:使用注册的 gmail 帐户登录。
不确定这个。我使用我的 gmail 帐户登录是的,但不是 ourSenderIdregistered@googlemail.com我们在注册时输入了 Intent 。我也坚信这不是解决方案。 (告诉我们所有的客户使用这个帐户登录......嗯,不?!)。
所以我猜这是别的东西,但我就是找不到它 :C

最佳答案

好吧,这真的很明显:

如果你想注册你使用的c2dm

com.google.android.c2dm.intent.REGISTER

但要获得此消息的答案,您必须设置广播接收器以收听

com.google.android.c2dm.intent.REGISTRATION

微妙?是的,这里再次出现错误和更正后的版本:

//false
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);
}


//true
public void registerC2DM(View v){
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", ourSenderIdregistered@googlemail.com);
startService(registrationIntent);
}

在意识到这一点之前砸了 3 个键盘......:)

关于android - C2DM 应用内注册 : unable to start service Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com