Android addProximityAlarm不';行不通

Android addProximityAlarm不';行不通,android,Android,我发现了数百个与同一个问题相关的主题,但我不明白哪里出了错!因为我基本上写了相同的代码 这是一项控制职位并添加承诺人的服务 public class GeoReminderService extends Service implements LocationListener{ private LocationManager locationManager; private final String proximityIntentAction = "com.example.geo.Proximi

我发现了数百个与同一个问题相关的主题,但我不明白哪里出了错!因为我基本上写了相同的代码

这是一项控制职位并添加承诺人的服务

public class GeoReminderService extends Service implements LocationListener{
private LocationManager locationManager;
private  final String proximityIntentAction = "com.example.geo.ProximityIntentReceiver";

private float latitude;
private float longitude;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 10, this);

    addProximityAlert(45.477872,9.23457);

    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onLocationChanged(Location location) {
    Log.v("Service","Location changed");
    if (location != null) {
            Log.v("Location changed : Lat: ", ""+location.getLatitude());
            Log.v("Location changed : Lon: ", ""+location.getLongitude());
    }
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

private void addProximityAlert(double latitude, double longitude) {
    Log.v("Service","proximity alert added" + latitude +" "+ longitude);        
    Intent intent = new Intent(proximityIntentAction);

    PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    locationManager.addProximityAlert(latitude, longitude, 3000, -1, proximityIntent);
    IntentFilter filter = new IntentFilter(proximityIntentAction); 
    registerReceiver(new ProximityIntentReceiver(), filter);
}
相反,这一个是应该捕获事件的接收器

public class ProximityIntentReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1000;

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("proximity receiver", "alert received");
    String key = LocationManager.KEY_PROXIMITY_ENTERING;

    Boolean entering = intent.getBooleanExtra(key, false);

    if (entering) {
        Log.v("ProximityIntentReceiver", "entering");
    }
    else {
        Log.v("ProximityIntentReceiver", "exiting");
    }       
}

即使我改变了位置(我使用的是Android模拟器),事件
onReceive
也不会触发。同时,我确信我正在正确地更改位置,因为事件
locationChanged
起作用。有人能帮我吗?

您在舱单上声明了ProximityIntentReceiver吗

在声明活动等的清单的同一区域中,您还需要声明您的广播接收者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.geo"
    android:versionCode="001"
    android:versionName="0.1"
    >
  <application>
    ...
    <receiver android:name=".ProximityIntentReceiver">
    </receiver>
  </application>
</manifest>

...

否,我只添加了权限!如何在舱单中声明接收人?对不起,我现在不能发布我的舱单。啊,这是你的问题。它也一直咬着我。我会编辑我的回答,它很有效!非常感谢,你的帮助非常有用!是的,就像我说的,它一直咬着我。您可能认为在intent中显式声明类就足够了,但事实证明,除非在清单中声明,否则Android不会调用接收方(或活动、或服务等)。