0

我想在我的应用程序中添加一个自定义广播接收器。我有 3 种方法可以进行广播。在我的 onReceive 方法中,我想确定广播的方法。我有3个这样的方法

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

这是我的 broadcastIntent 方法

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

在我的 onReceive 方法中,我使用 getFlags() 方法从意图中获取标志值并通过 if 发送它,否则。但这不起作用。欢迎任何改进建议。

4

3 回答 3

2

第一个问题是 ypu 没有为您的意图指定目标。您可以使用建议的意图过滤器和操作,如 rodkarom 或直接指定接收者的类(参见我的示例)。在这两种情况下,您都需要在AndroidManifest.xml中声明您的广播接收器,或者在运行时注册它(请参阅 rodkarom 的答案以获取示例)。

该方法addFlags用于指定 Intent 的一些内部属性(例如在新任务中启动与此 Intent 对应的活动),因此您不能将其用于自己的数据。可能的标志列表在setFlags方法的文档中。

您可以使用putExtra来实现您的目标:

// 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) {
            // ...
        }
        // ...
    }
}
于 2013-05-07T07:19:15.940 回答
2

我找到了解决方法并想到了共享代码。这是我在主要活动中针对 2 种不同方法进行的广播。

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

这就是我收到它的方式..

public class Receiver extends BroadcastReceiver {

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

这就是我在清单上注册它的方式。

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

如果其他人遇到类似问题,希望这会有所帮助。非常感谢在这里发布答案的每一个人。

于 2013-05-09T05:24:27.470 回答
2

您还可以使用 Actions 来识别每个 Intent 对象。

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

在您的onReceive方法中,您可以使用getAction()意图方法来检查发送了哪些操作。这是在您尚未使用操作的情况下。

[[编辑]]

要注册一个BroadcastReceiver,您需要定义一个IntentFilter并注册您希望以这种方式接收的操作:

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
    }
}
于 2013-05-07T07:26:55.123 回答