2014-01-25 83 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";