Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit addressing an Intent to a dynamically broadcast receiver

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

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.

implicit Intent intent.setAction("com.example.myChatMessage")

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

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

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

My Question: Is it possible to explicit adress a dynamicall registered broadcast receiver?

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

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

like image 889
Jens S. Avatar asked Feb 11 '13 10:02

Jens S.


1 Answers

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.

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.

Source: http://onemikro2nd.blogspot.com/2013/09/darker-corners-of-android.html

like image 198
d2vid Avatar answered Oct 14 '22 18:10

d2vid