开发者

infinitly execution while send broadcast

开发者 https://www.devze.com 2023-02-27 10:20 出处:网络
I want to use context.sendBroadcast(intent, receiverPermission); in my application but I don\'t know to pass receiverPermission parameter in function and also how to set in manifest file

I want to use

context.sendBroadcast(intent, receiverPermission);

in my application but I don't know to pass receiverPermission parameter in function and also how to set in manifest file please any body help me

I want to show you my source code

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->sendL开发者_Python百科ocation update broadcast");
                i1.putExtra("Latitude", loc.getLatitude());
                i1.putExtra("Longitude", loc.getLongitude());
                context.sendBroadcast(i1,null);
            }
        });
    }
}

and on activity I have write

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

Now my problem is when it sendbroadcast it execute infinitly doBroadcast function(), please help me to come out.


  1. Check after intent.setAction(BROADCAST_ACTION); that the action is really set to BROADCAST_ACTION
  2. Check if you have registered this BroadcastReceiver with the action BROADCAST_ACTION in the <intent-filter> ( if it is, then that s why you have that infinite loop)
0

精彩评论

暂无评论...
验证码 换一张
取 消