tags:

views:

819

answers:

2

Hi I'm trying to get and update when the phone gets near a location. I've both tried using addProximityAlert and requestLocationUpdates

    LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location l = lm.getLastKnownLocation("gps");
    Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    lm.addProximityAlert(12, 12, 100, 1000000, pIntent);

This one never fires the intent (which I know works since I can fire it up manually). I tried using a listener but it gets executed only once. No matter how many times I update the gps it never gets called again

    ProximityListener pl = new ProximityListener();
    lm. requestLocationUpdates("gps", 2000, 10, pl);

this is the code of the activity (called in the first case)

public class ProximityAlert extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 Log.d("intent", "Hi");
 finish();
}

This is the Listener

public class ProximityListener implements LocationListener {
String DEBUG_TAG = "ProximityListener";
@Override
public void onLocationChanged(Location location) {
 // TODO Auto-generated method stub
 Log.d(DEBUG_TAG, location.toString());

}

@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

}
}

Thanks

A: 

I don't think you should terminate the Activity already in the onCreate method (by calling finish()).

that was just a debug activity and I was interested in seeing if it writes on the Log, so finishing the activity doesn't change much.But thanks for the answer
MaurizioPz
+1  A: 

I think the problem is how you define you Intent / PendingIntent. There are two ways to start an Activity using an Intent, and the code you've included looks like a cross between the two.

The standard way of starting an Activity is to use the Intent constructor that takes the current context and the Activity's class and use the getActivity method on PendingIntent to create the PendingIntent:

Intent intent = new Intent(this, ProximityAlert.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

Alternatively, you can add an IntentReceiver to your Activity in the Manifest, with an IntentFilter that listens for a particular action (like "eu.mauriziopz.gps.ProximityAlert"). However, in that case you need to use PendingIntent.getBroadcast to create the PendingIntent.

Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

In all cases you need to make sure you've got the correct permissions for location-based services defined in your manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>


Also, rather than using the string "gps", you might consider using the static constant LocationManager.GPS_PROVIDER.

Reto Meier