简体   繁体   中英

Multiple custom broadcasting (how to handle)

I want to add a custom broadcast receiver to my app. and I have 3 methods that should do the broadcast. and in my onReceive method i want to identify which method did the broadcast. I have 3 methods like this

public void method01(View v){
    int flag = 1;
    Intent intent = new Intent();
    intent.addFlags(flag);
    broadcastIntent(intent);
}

public void method02(){
    int flag = 2;
    Intent intent = new Intent();
    intent.addFlags(flag);
    broadcastIntent(intent);    
}

public void method03(){
    int flag = 3;
    Intent intent = new Intent();
    intent.addFlags(flag);
    broadcastIntent(intent);
}

and this is my broadcastIntent method

public void broadcastIntent(Intent intent){
    sendBroadcast(intent);
}

in my onReceive method i use getFlags() method to get the flag value from the intent and send it through a if, else. but this do not work. any suggestion for improvements are welcome.

The first problem is that ypu didn't specify a target for your intent. You can use intent filters and actions like rodkarom suggested or specify receiver's class directly (see in my example). In both cases you need either to declare your broadcast receiver in AndroidManifest.xml , or register it at runtime (see rodkarom's answer for a sample).

The method addFlags is used to specify some internal properties of Intent (like start activity corresponding to this intent in a new task) , so you cannot use it for your own data. The list of possible flags is in the documentation for setFlags method.

You can use putExtra to achieve your goal:

// an Activity is just an example
public class SenderActivity extends Activity {

    // ...        
    void method01() {
        int flag = 1;
        Intent intent = new Intent(getApplicationContext(), Receiver.class); // any Context is acceptable here
        intent.putExtra(MyReceiver.EXTRA_FLAG, flag); // any string will do well, you just need it to be the same here and in getExtra later
        sendBroadcast(intent);
    }
}


public class MyReceiver extends BroadcastReceiver {    
    public static final String EXTRA_FLAG = "your.package.name.EXTRA_FLAG";

    // and in onReceive
    public void onReceive (Context context, Intent intent) {
        int flag = intent.getIntExtra(EXTRA_FLAG, someDefaultValue);
        if (flag == 1) {
            // ...
        }
        // ...
    }
}

You can also use Actions to identify each one of you Intent objects.

String action1 = "first_sender";
String action2 = "second_sender";

Intent createIntent01(){
Intent intent01 = new Intent();
intent01.setAction(action1);
return intent01;
}
Intent createIntent02(){
Intent intent02 = new Intent();
intent01.setAction(action2);
return intent02;
}

And in your onReceive method you can use the getAction() method of intents to check which action was sent. This is in case you're not already using Actions.

[[EDIT]]

To register a BroadcastReceiver you need to define an IntentFilter and register the actions you wish to receive this way:

mBroadcastReceiver broadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action1);
intentFilter.addAction(action2);
registerReceiver(broadcastReceiver,intentFilter);

class mBroadcastReceiver extends BroadcastReceiver{
    public void onReceive(Context arg0, Intent arg1){
    String action = arg1.getAction();
    if(action.equals(action1)){
        //do something
    }else if(action.equals(action2)){
       //do something else
    }
}

I found out the way to this and thought of sharing the code. this is the broadcasting done in my main activity for 2 different methods.

public void method1(View view){
    Intent intent = new Intent();
    intent.setAction("method1");
    sendBroadcast(intent);
}

public void method2(View view){
    Intent intent = new Intent();
    intent.setAction("method2");
    sendBroadcast(intent);
}

and this is how i receive it..

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Broadcast Intent Detected.",Toast.LENGTH_LONG).show();
}

this is how i registered it on manifest.

 <receiver android:name="Receiver" >
        <intent-filter>
            <action android:name="method1" >
            </action>
            <action android:name="method2" >
            </action>
        </intent-filter>
    </receiver>

Hope this will help if any one else came up with similar problem. and big thank you to every one who posted their answers here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM