PDA

View Full Version : [Android] addProximityAlert non funziona


oNaSsIs
18-04-2013, 17:22
Ho trovato centinaia di topic con persone che hanno il mio stesso problema. C'è qualcuno che dice di essere riuscito a risolvere, ma non capisco come dato che non mi sembra facciano nulla di verso da quello che faccio io.

Questo è un servizio che monitora la posizione e aggiunge un proximityAlert

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);
}
}


Questo invece è il Reciver che dovrebbe catturare l'evento.

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");
}
}
}

Quello che succede è che nonostante io cambi la posizione (sto testando l'applicazione attraverso l'emulatore) il metodo onReceive non viene mai chiamato. Allo stesso tempo però l'evento locationChanged, viene correttamente catturato e infatti dal log vedo che la posizione cambia correttamente. Secondo voi dove sbaglio?

oNaSsIs
20-04-2013, 19:24
Soluzione (http://stackoverflow.com/questions/16093891/android-addproximityalarm-doesnt-work/16094197?noredirect=1#comment23027896_16094197)