如何使用sendOrderedBroadcast函数

时间:2011-04-20 05:27:18

标签: android

我想用

context.sendOrderedBroadcast(intent, receiverPermission)context.sendBroadcast(intent, receiverPermission);

在我的申请中 但我不知道在函数中传递receiverPermission参数以及如何在清单文件中设置 请任何身体帮助我

我想向您展示我的源代码

public class LocationReceiver extends BroadcastReceiver {
    public static final String BROADCAST_ACTION = "LOCATION_CHANGE";
    @Override
    public void onReceive(Context context, Intent intent) {
        intent.setAction(BROADCAST_ACTION);
        Bundle b = intent.getExtras();
        Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
        Logger.debug("Loc:"+loc);
        if(loc != null){
            doBroadCast(context,intent,loc);
        }
    }

    public void doBroadCast(final Context context,final Intent i1,final Location loc){
        Handler h = new Handler();
        h.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Logger.debug("LocationReceiver->sendLocation update broadcast");
                i1.putExtra("Latitude", loc.getLatitude());
                i1.putExtra("Longitude", loc.getLongitude());
                context.sendBroadcast(i1,null);
            }
        });
    }
}

以及我写的活动

  @Override

        protected void onResume() {
            registerReceiver(broadcastReceiver, new IntentFilter(LocationReceiver.BROADCAST_ACTION));
}

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                UpdateUI(intent);
            }
        };

        private void UpdateUI(Intent i){
            Double Latitude = i.getDoubleExtra("Latitude",0); 
            Double Longitude = i.getDoubleExtra("Longitude",0);
            showMap(Latitude, Longitude);
        }

现在我的问题是当它发送广播它无限执行doBroadcast功能()时,请帮我出来。

1 个答案:

答案 0 :(得分:1)

Please refer SDK document

receiverPermission是(可选)字符串命名接收者必须持有的权限才能接收您的广播。如果为null,则不需要权限。

AndroidManifest.xml use.这是在String上。 String使用sendOrderedBroadcast的receiverPermission。值。