use*_*618 36 android android-c2dm
我即将为我的应用程序实现C2DM,但我发现有关如何编写清单的文档有点令人困惑.
清单代码示例包含:
<!-- Only this application can receive the messages and registration result --> 
<permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" />
这解释如下:
applicationPackage +".permission.C2D_MESSAGE阻止其他应用程序注册和接收应用程序的消息.
但这究竟是如何工作的呢?据我所知,这声明了一个权限,然后获得我的应用程序的权限.但是这个许可究竟在哪里执行?
注册的代码是:
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", emailOfSender);
startService(registrationIntent);
接收registrationIntent的服务如何知道要检查的权限?据我所知(如果我错了,请纠正我),在声明权限时,我可以在我的命名空间中选择任何权限名称,例如com.example.myapp.permission.WHATEVER.
或者C2D_MESSAGE是我必须使用的魔法常数?
此外,文档说我必须实现接收器com.google.android.c2dm.intent.C2D_MESSAGE和com.google.android.c2dm.intent.REGISTRATIONIntents.但在代码示例中,接收者的过滤器仅包含.intent.RECEIVE和.intent.REGISTRATIONIntents.C2D_MESSAGE去哪儿了,它和我上面的问题有什么关系吗?
我希望这不是显而易见的事情,但我只是不明白......请帮忙.
mba*_*ord 24
他们声称您在摘要顶部需要的接收器似乎是文档中的错误,因为它与清单示例不匹配.
"com.google.android.c2dm.intent.C2D_MESSAGE和com.google.android.c2dm.intent.REGISTRATION的接收者.")
我使用简单的示例清单条目调整了他们给工作的示例,以匹配我的特定应用程序的包名称.
当Android中的C2DM代码发送广播时(或者,当它寻找潜在的接收者发送广播时),它会查找:
查找Intent的广播接收器:com.google.android.c2dm.intent.REGISTRATION 具有以下权限:.permission.C2D_MESSAGE
通过Android C2DM代码发送到您的应用程序的所有C2DM消息都将查找该特定权限.它特定于您的应用程序,只需要在该结构中(您的应用程序的包+".permission.C2D_MESSAGE").
关于注册请求如何获取用于权限的包名称,这是在您首次向"app"额外注册时处理的 - 来自文档:
"app是应用程序的ID,使用PendingIntent设置,以允许注册服务提取应用程序信息."
有问题的一行:
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
| 归档时间: | 
 | 
| 查看次数: | 54204 次 | 
| 最近记录: |