发送广播时无限执行

时间:2011-04-20 06:12:36

标签: android

我想用
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 :(得分:0)

  1. intent.setAction(BROADCAST_ACTION);之后检查该操作是否真的设置为BROADCAST_ACTION
  2. 检查您是否已在BroadcastReceiver中使用BROADCAST_ACTION的{​​{1}}注册此<intent-filter>(如果是,那就是为什么您有无限循环)