明确解决的意向动态广播接收器 [英] Explicit addressing an Intent to a dynamically broadcast receiver

查看:136
本文介绍了明确解决的意向动态广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,并试图了解应用程序之间的通信。

i am new to Android and trying to understand the communication between apps.

我想写3可以相互通信的小应用程序。如果您想发送一条消息给大家,你只需要使用一个隐含的播放。

I am trying to write 3 little apps which can communicate with each other. If you want to sent a message to everybody you just use an implicit broadcast.

隐含的意图 intent.setAction(com.example.myChatMessage)

如果你想解决只有1 specifc接收我做到了以

if you want to adress only 1 specifc receiver i did it with

explicite意图 intent.setComponent(com.example.test.android.broadcastreceiver.b com.example.test.android.broadcastreceiver.b.myBroadcastReceiver)

explicite Intentintent.setComponent("com.example.test.android.broadcastreceiver.b", "com.example.test.android.broadcastreceiver.b.myBroadcastReceiver")

这工作,当广播接收器是一个独立的类,并在AndroidManifest.xml中定义。

this works, when the broadcast receiver is a seperate class and defined in the AndroidManifest.xml.

我的提问:是有可能的明确的联系地址一dynamicall注册的广播接收器

package com.example.test.android.broadcastreceiver.b;

public class MainActivity extends Activity {

private final IntentFilter intentfilter = new IntentFilter("com.example.myChatMessage");
private myBroadcastReceiver broadcastreceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    broadcastreceiver = new myBroadcastReceiver();
    registerReceiver(broadcastreceiver, intentfilter);
}

public static class myBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
        Log.d("message", "B received: "+message);       
    }
}
}

它接收所有隐式广播,但没有明确的关系 - 我不知道烫ADRESS它。你能帮助我吗?

It receives all implicit broadcasts but no explicit one - i don't know hot to adress it. Can you help me?

推荐答案

它似乎不是可以发送一个明确的意图动态注册的广播接收器。注册在AndroidManifest.xml中的接收器是唯一的办法。

It does not appear possible to send an explicit intent to a dynamically registered broadcast receiver. Registering the receiver in AndroidManifest.xml is the only way.

如果你动态地注册一个BroadcastReceiver - 通过调用Context.registerReceiver() - 你提供一个BroadcastReceiver的实例的......如果您尝试通过命名类的BroadcastReceiver送的意图接收器,它永远不会被传递.. Android系统不会你申报的类,你注册的BroadcastReceiver实例的意图相匹配。

If you dynamically register a BroadcastReceiver – by calling Context.registerReceiver() – you supply a BroadcastReceiver instance ... If you try to send an Intent to the receiver by naming the class of the BroadcastReceiver, it will never get delivered .. The Android system will not match the Intent you declared to the class of the BroadcastReceiver instance you registered.

来源:<一href="http://onemikro2nd.blogspot.com/2013/09/darker-corners-of-android.html">http://onemikro2nd.blogspot.com/2013/09/darker-corners-of-android.html

这篇关于明确解决的意向动态广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆