簡體   English   中英

多種自定義廣播(如何處理)

[英]Multiple custom broadcasting (how to handle)

我想向我的應用添加自定義廣播接收器。 我有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,else將其發送。 但這不起作用。 歡迎提出任何改進建議。

第一個問題是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) {
            // ...
        }
        // ...
    }
}

您還可以使用操作來標識每個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
    }
}

我找到了解決方法,並想到了共享代碼。 這是我主要活動中針對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>

希望這對其他人遇到類似問題也能有所幫助。 非常感謝在這里發布答案的每個人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM