C2DM接收器的使用registerReceiver动态寄存器 [英] Dynamic register of C2DM receiver using registerReceiver
问题描述
我可以成功地使用我的注册与C2DM的Android应用程序中的<接收器>
在我的清单。不过,如果我删除<接收器>
从清单和使用上下文的方法registerReceiver登记自己的接收器,我收到了 SERVICE_NOT_AVAILABLE
错误响应。我在模拟器和真实设备复制这种行为。
时的动态注册一个C2DM接收器可能吗?
这是我删除清单的片段:
<接收机器人:service.C2DM.C2DMReceiver名称=机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<类机器人:名称=mytestapp/>
&所述; /意图滤光器>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类机器人:名称=mytestapp/>
&所述; /意图滤光器>
< /接收器>
这里是code:
公共类C2DMReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串登记= intent.getStringExtra(registration_id);
如果(intent.getStringExtra(错误)!= NULL){
// TODO:注册失败,应稍后再试。
Log.e(MyTestAppC2DM,C2DM错误=+ intent.getStringExtra(错误));
}否则,如果(intent.getStringExtra(未登记)!= NULL){
Log.d(MyTestAppC2DM,未注册C2DM);
}否则,如果(注册!= NULL){
Log.d(MyTestAppC2DM,C2DM registration_id =+登记);
} 其他 {
Log.w(MyTestAppC2DM,C2DM否registration_id);
}
}
公共静态无效寄存器(上下文的背景下){
/ * BEGIN动态寄存器* /
C2DMReceiver c2dmReceiver =新C2DMReceiver();
IntentFilter的receiveIntentFilter =新的IntentFilter();
receiveIntentFilter.addAction(com.google.android.c2dm.intent.RECEIVE);
receiveIntentFilter.addCategory(mytestapp);
context.registerReceiver(c2dmReceiver,receiveIntentFilter,com.google.android.c2dm.permission.SEND,NULL);
IntentFilter的registrationIntentFilter =新的IntentFilter();
registrationIntentFilter.addAction(com.google.android.c2dm.intent.REGISTRATION);
registrationIntentFilter.addCategory(mytestapp);
context.registerReceiver(c2dmReceiver,registrationIntentFilter,com.google.android.c2dm.permission.SEND,NULL);
/ * END动态寄存器* /
意图registrationIntent =新的意向书(com.google.android.c2dm.intent.REGISTER);
registrationIntent.putExtra(应用程序,PendingIntent.getBroadcast(上下文,0,新意图(),0));
registrationIntent.putExtra(发件人,mysender@gmail.com);
组件名服务= context.startService(registrationIntent);
如果(服务!= NULL){
Log.d(MyTestAppC2DM,C2DM登记发送);
}其他{
Log.d(MyTestAppC2DM,C2DM服务没有找到);
}
}
}
在一个广播系统发送的,将启动应用程序来处理这个意图,如果他们有在清单中声明的匹配意图过滤器。由于C2DM的性质,它是没有用的具有动态安装的广播接收器,因为接收到C2DM消息时您的应用程序可能无法运行。如果它不运行它不会被启动了动态安装接收器。
I can register my android app with C2DM successfully using a <receiver>
in my manifest. However, if I delete the <receiver>
from the manifest and register my receiver using the method registerReceiver of the context, I receive a SERVICE_NOT_AVAILABLE
error response. I have reproduced this behaviour in the emulator and in a real device.
Is dynamically registering a C2DM receiver possible?
This is the fragment of the manifest I deleted:
<receiver android:name=".service.C2DM.C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="mytestapp" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="mytestapp" />
</intent-filter>
</receiver>
And here is the code:
public class C2DMReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String registration = intent.getStringExtra("registration_id");
if (intent.getStringExtra("error") != null) {
//TODO: Registration failed, should try again later.
Log.e("MyTestAppC2DM", "C2DM Error = " + intent.getStringExtra("error"));
} else if (intent.getStringExtra("unregistered") != null) {
Log.d("MyTestAppC2DM", "C2DM Unregistered");
} else if (registration != null) {
Log.d("MyTestAppC2DM","C2DM registration_id = " + registration);
} else {
Log.w("MyTestAppC2DM", "C2DM No registration_id");
}
}
public static void register(Context context){
/* BEGIN OF DYNAMIC REGISTER */
C2DMReceiver c2dmReceiver = new C2DMReceiver();
IntentFilter receiveIntentFilter = new IntentFilter();
receiveIntentFilter.addAction("com.google.android.c2dm.intent.RECEIVE");
receiveIntentFilter.addCategory("mytestapp");
context.registerReceiver(c2dmReceiver, receiveIntentFilter, "com.google.android.c2dm.permission.SEND", null);
IntentFilter registrationIntentFilter = new IntentFilter();
registrationIntentFilter.addAction("com.google.android.c2dm.intent.REGISTRATION");
registrationIntentFilter.addCategory("mytestapp");
context.registerReceiver(c2dmReceiver, registrationIntentFilter, "com.google.android.c2dm.permission.SEND", null);
/*END OF DYNAMIC REGISTER*/
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "mysender@gmail.com");
ComponentName service = context.startService(registrationIntent);
if(service!=null){
Log.d("MyTestAppC2DM","C2DM Registration sent");
}else{
Log.d("MyTestAppC2DM","C2DM Service not found");
}
}
}
When a broadcast is sent by the system it will start applications to handle the intent if they have a matching intent filter declared in the manifest. Due to the nature of C2DM it is not useful to have a Broadcast Receiver installed dynamically because your application might not be running when a C2DM message is received. If its not running it won't be started for a dynamically installed receiver.
这篇关于C2DM接收器的使用registerReceiver动态寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!