我设置了一个广播接收器,但它没有收到我发送的广播消息:
我的清单
<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 :(得分:2)
更改此行:
public static final String LOCATION_BROAD_MSG = "com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG";
您已在接收方注册com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG
作为意图过滤器。
答案 1 :(得分: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";