2014-01-25 88 views
0

我已經設置的廣播接收器,但它沒有得到廣播消息我發送:廣播接收器不響應廣播消息

我的清單

<receiver android:name="BootReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <action android:name="com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG" /> 
    </intent-filter> 
</receiver> 

發送(調試顯示我到達發送線路)

public class LocationService extends Service { 
public static final String LOCATION_BROAD_MSG = "Hello"; 
private static final int TWO_MINUTES = 1000 * 60 * 2; 

public LocationManager locationManager; 
public MyLocationListener listener; 
public Location previousBestLocation = null; 

Intent intent; 
int counter = 0; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    intent = new Intent(LOCATION_BROAD_MSG);  
} 

@Override 
public void onStart(Intent intent, int startId) {  

} 

    public class MyLocationListener implements LocationListener 
    { 

     public void onLocationChanged(final Location loc) 
     { 
     intent = new Intent(LOCATION_BROAD_MSG); 
      Log.i("**************************************", "Location changed"); 
      if(isBetterLocation(loc, previousBestLocation)) { 
       intent.putExtra("Latitude", loc.getLatitude()); 
       intent.putExtra("Longitude", loc.getLongitude());  
       intent.putExtra("Provider", loc.getProvider());     
       sendBroadcast(intent);   

      }        
     } 

} 

接收(的onReceive調試過程中從未達到過)

public class BootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent intent) { 
     // TODO Auto-generated method stub 


     if (intent != null) 
     { 

      String action = intent.getAction(); 

      if (action != null) 
      { 
       if (action.equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) 
       { 
              // Log.d("receiver","action is: boot"); 
       } 

       if (action.equalsIgnoreCase(LocationService.LOCATION_BROAD_MSG)) 
       { 
         //Toast.makeText(, "wee hee", Toast.LENGTH_LONG); 
       } 

      } 
      } 
    } 

} 
+0

嗨,你有沒有試圖把你的onReceive方法的根目錄?我可以看到你有一個但是在if語句中,並且你能告訴我們你的android軟件包中的清單文件和LOCATION_BROAD_MSG常量是什麼值嗎?這可能是從清單BootReceive不知道在哪裏是實際的類.. – Cata

+0

添加了一些更多的代碼 –

回答

2

改變這一行:

public static final String LOCATION_BROAD_MSG = "com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG"; 

,您已經註冊com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG在接收器的意圖過濾器。

0

的問題是,你正在註冊的廣播接收來自消息: com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG和您發送這樣的: LOCATION_BROAD_MSG = "Hello" ..

你應該有

public static final String LOCATION_BROAD_MSG = "com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG";