2011-04-20 221 views
3

我想用如何使用sendOrderedBroadcast功能

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

現在我的問題是,當它sendbroadcast它infinitly執行了DoBroadcast()函數,請幫我走出來。

回答

0

Please refer SDK document

receiverPermission是(可選)字符串命名的權限,一個接收器必須以收到您的廣播舉行。如果爲null,則不需要權限。

AndroidManifest.xml use.這是在String上。該字符串使用sendOrderedBroadcast的receiverPermission。值。