AddProximityAlert not working (also not requesting LocationUpdates) - Android

I try to get and update when the phone gets closer to the location. I 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 never works against intent (which I know, since I can start it manually). I tried using a listener, but it only executes once. No matter how many times I update gps, it will never be called again

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

this is the action code (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 receiver

 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

+4
source share
4 answers

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

The standard way to start an Activity is to use the Intent constructor, which takes the current context and the Activity class, and use the getActivity method on the 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 using an IntentFilter that listens for a specific action (for example, "eu.mauriziopz.gps.ProximityAlert"). However, in this 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 that you have the correct permissions for the 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"/> 

Alternatively, instead of using the string "gps", you can use the static constant LocationManager.GPS_PROVIDER .

+5
source
 Intent intent = new Intent("com.sccp.fourgdummy.REQUEST_DFGS"); intent.putExtra("launch_tab", "otp"); PendingIntent proximityIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 

this is important: Intent.FLAG_ACTIVITY_NEW_TASK

+1
source

The API defines addProximityAlert parameters as lat, long, radius, expiration, intent. Have you tried setting expiration -1 , not 100000

lm.addProximityAlert(12, 12, 100, 1000000, pIntent); Strike>

 lm.addProximityAlert(12, 12, 100, -1, pIntent); 
+1
source

I do not think that you should terminate the action already in the onCreate method (by calling finish ()).

0
source

All Articles