Broadcastreceiver for Location

BroadcastReceiver for location

I think what you are looking for is something like this. There is a version of LocationManager.requestLocationUpdates() that takes a PendingIntent as a parameter, and this Intent could be used to fire a BroadcastReceiver. This code will register a custom broadcast to fire with the location updates.

Intent intent = new Intent("UNIQUE_BROADCAST_ACTION_STRING_HERE");
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
long minTime; //This should probably be fairly long
float minDistance; //This should probably be fairly big
String provider; //GPS_PROVIDER or NETWORK_PROVIDER

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.requestLocationUpdates(provider, minTime, minDistance, launchIntent);

Then you just have to register your BroadcastReceiver with the same Intent action and it will receive the location updates for processing. And, of course, keep that PendingIntent around because you'll need to call manager.removeUpdates(launchIntent) at some point!

Final Note:

Because it sounds like you are trying to implement regular location updates while your app isn't in the foreground, keep this in mind. Unless you want your app branded as a battery killer, be extremely careful with how you implement this functionality.

You will want to greatly reduce the frequency up location updates with either a large minTime or minDistance parameter to allow the location service to rest. This feature should also be attached to a user controlled preference so they can enable/disable your app from tracking location in the background.

Hope that Helps!

How to trigger broadcast receiver when gps is turn on/off?

This is useful when user want to trigger any action on turn On/Off location provides

You should add this action in manifest

<action android:name="android.location.PROVIDERS_CHANGED" />

and after add this action you can trigger your broadcast receiver

<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

And in your BroadcastReceiver class you have to implement LocationListener in that class which is given following below..

public class GpsLocationReceiver extends BroadcastReceiver {        
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
}

android BroadCastReceiver for location

take a look at LocationManager, there is a broadcast send when a Provider gets enabled/disabled. There is also a method to check all available location providers and if they are enabled.

Get GPS Location in a Broadcast Receiver/or Service to Broadcast Receiver data transfer

From BroadcastReceiver I could not start Location Manager as well. So I started a service on BroadcastReceiver and in that service I manipulated Location Manager. I think I found this solution in Android development documentation. You also can start Activity instead of Service.

Here is the code of switch to Service on BroadcastReceiver:

write this in onReceive method

    Intent serviceIntent = new Intent(context,MyService.class);
serviceIntent.putExtra("locateRequest", "locateRequest"); // if you want pass parameter from here to service
serviceIntent.putExtra("queryDeviceNumber", results[1]);
context.startService(serviceIntent); //start service for get location

How with broadcast receiver should I subscribe to listen for location services change

This is useful when user want to trigger any action on turn On/Off location provides

You should add this action in manifest

<action android:name="android.location.PROVIDERS_CHANGED" />

and after add this action you can trigger your broadcast receiver

<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

And in your BroadcastReceiver class you have to implement LocationListener in that class which is given following below..

  public class GpsLocationReceiver extends BroadcastReceiver {        
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
//NO GPS
}else{
//Gps is on
}
}
}
}

BroadcastReceiver for Google Play Location Service (setting of the Status Bar)

Well, I found one way to check if the "Location" setting of the action bar is enabled or disabled; but without a BroadcastReceiver (a shame, really)

private static final String TAG = getClass().getName();

/* We define the LocationManager */
LocationManager location_Manager= (LocationManager) getSystemService(LOCATION_SERVICE);

/* If the GPS provider or the network provider are enabled; the Location setting is enabled.*/
if(location_Manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || location_Manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG, "The Location setting is enabled");
}else{
/* We send the user to the "Location activity" to enable the setting */
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}

I really did not need a BroadcastReceiver; since the "arquitecture" that has my app allows me to do without it; but I would have liked to know how to use it.

NOTICE:

If someone finds the way to make it with a BroadcastReceiver I will change my correct answer by her answer.



Related Topics



Leave a reply



Submit