I want to use 开发者_如何学编程
context.sendOrderedBroadcast(intent, receiverPermission)
or 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->sendLocation 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.
Please refer SDK document
receiverPermission is (optional) String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required.
AndroidManifest.xml use. this is on String. the String use sendOrderedBroadcast's receiverPermission. value.
精彩评论