Android – sendOrderedBroadcast帮助

前端之家收集整理的这篇文章主要介绍了Android – sendOrderedBroadcast帮助前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的 Android应用中使用 sendOrderedBroadcast.

我希望能够将Intent从我的一个应用程序发送到另一个应用程序,然后我想从收到Intent的应用程序中获取数据,在这种情况下为布尔值true或false.

这是当前的代码

Intent i = new Intent();
    i.setAction(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT);
    i.putExtra("com.testnetworks.QCLEVEL",aProposedTheoreticalQoSLevel);
    sendOrderedBroadcast(i,null,Activity.RESULT_OK,null);

这是实现我想要的正确方法吗?

如果是这样,我将什么用作resultReceiver *参数? (第3个参数)

然后我如何从广播中收回数据?

我做了一个快速的谷歌,没有提出任何例子,任何帮助或例子非常感谢.

更新的代码

sendOrderedBroadcast(i,domainBroadcast,null);
class DomainBroadcast extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0,Intent intent) {
        String action = intent.getAction();

        if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){
            Log.d("BROADCAST","Returning broadcast");

            Bundle b = intent.getExtras(); 
            Log.d("BROADCAST","Returning broadcast " + 
                    b.getInt("com.testnetworks.INT_TEST"));
        }      
    }
@Override
    public void onReceive(Context context,Intent intent) {
            String action = intent.getAction(); 

            if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){

                Bundle b = intent.getExtras();
                int testQCLevel = b.getInt("com.testnetworks.QCLEVEL");
                switch(testQCLevel){
                case 1:
                    Log.d("QCLevel ","QCLevel = UNAVAILABLE");
                    break;
                case 2:
                    Log.d("QCLevel ","QCLevel = BELOWUSABILITY");
                    break;
                case 3:
                    Log.d("QCLevel ","QCLevel = VOICE");
                    break;
                }

                intent.putExtra("com.testnetworks.INT_TEST",100);

          }

所以根据Doc的说法,我应该在我的DomainBroadcast接收器中收到100,但它总是回到0.

谁能明白为什么?

** resultReceiver – 您自己的BroadcastReceiver将其视为广播的最终接收者.*

解决方法

您需要 get the extra data results Bundle并将数据添加到其中.

尝试这样的事情:

public class DemoOrderedBroadcast extends Activity {
    private static String SOMETHING_HAPPENED = "com.example.somethinghappened";
    private static String EXTRA_INTEGER = "extra integer";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter filter = new IntentFilter(SOMETHING_HAPPENED);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                Bundle results = getResultExtras(true);
                results.putInt(EXTRA_INTEGER,100);
                Log.d("DemoOrderedBroadcast","In Initial Receiver: Put 'extra integer' = 100");
            }
        },filter);

        Intent intent = new Intent(SOMETHING_HAPPENED);
        sendOrderedBroadcast(intent,new BroadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                Bundle results = getResultExtras(true);
                Log.d("DemoOrderedBroadcast","In Result Receiver: Got 'extra integer' = "
                                + results.getInt(EXTRA_INTEGER,-1));
            }
        },null);
    }
}

这产生了所需的输出

$adb -e shell am start -n com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast
Starting: Intent { cmp=com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast }
$adb -e shell logcat | grep D/DemoOrderedBroadcast
D/DemoOrderedBroadcast( 1343): In Initial Receiver: Put 'extra integer' = 100
D/DemoOrderedBroadcast( 1343): In Result Receiver: Got 'extra integer' = 100

猜你在找的Android相关文章