2011-04-20 58 views
0

我想在我的應用程序 使用
context.sendBroadcast(intent, receiverPermission);infinitly執行,同時發送廣播

,但我不知道通過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函數()時,請幫我出來。

+0

1.第一個廣播的啓動代碼是什麼樣的? 2.您在Manifest中爲您的LocationReceiver類設置了什麼意圖過濾器? – Maximus 2011-04-20 06:30:57

回答

0
  1. 檢查intent.setAction(BROADCAST_ACTION);的動作實在是設置爲BROADCAST_ACTION
  2. 檢查您是否已經註冊該BroadcastReceiver與在<intent-filter>動作BROADCAST_ACTION(如果是的話,那就是爲什麼你有無限循環)