How to stop the "searching for GPS" in android - android

i am trying to stop the "searching for GPS" in my app.
when i am using this line of code:
this.locationManager.removeUpdates(this);
i can still see the icon of the GPS in the notification bar, and its not stopping at all.
what else can i do?
Thanks!

Instead of passing this you should pass the location Listener instance to removeUpdate method like this.
locationManager.removeUpdates(gpsLocationListener);
where
GPSLocationListener gpsLocationListener = new GPSLocationListener();
class GPSOnChangeLocationListener implements LocationListener {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}

i figured it out.
thats what i did and it works:
private void registerLocationUpdates() {
Intent intent = new Intent(ParkOGuardActivity.class.getName()
+ ".LOCATION_READY");
pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, intent, 0);
// minimum every 30 minutes, 300 kilometers
this.locationManager.requestLocationUpdates(this.provider, 30000,
300000, pendingIntent);
}
private void cancelLocationUpdates() {
this.locationManager.removeUpdates(pendingIntent);
}
Thanks to all of you who tried to help me.

try locationManager.removeUpdates(gpsl);

Related

RequestLocationUpdates Time of life

RequestLocationUpdates is a method of LocationManager, receives GPS information periodically.
I think if I send from onCreate application should not be a problem because it will not overcharge the main thread, I'm right?
If I want to receive information requestLocationUpdates, even after the application closed, from where should I send it?
I think for the most part you will want to register a BroadcastReceiver. one of the broadcasts that you can watch for is when the location changes. There are a few logistical concerns with this such as how to interact with the BroadcastReceiver and how much battery your application will consume. A good summary of how to address these concerns can be found here.
Essentially you will want to make an Intent (your specific way of identfying an event you're looking for has happened) and a PendingIntent(A way to make that event interact with LocationServices).
PendingIntent launchIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
manager.requestLocationUpdates(provider, minTime, minDistance, launchIntent);
In the end the best solution I found was to use Service, as follows:
public class GpsService extends Service implements LocationListener{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocationManager LocManager =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this);
return Service.START_STICKY;
}
public void onLocationChanged(Location location) {
Log.i("location", String.valueOf(location.getLongitude()));
Log.i("location", String.valueOf(location.getLatitude()));
}
public void onProviderDisabled(String provider) {
Log.i("info", "Provider OFF");
}
public void onProviderEnabled(String provider) {
Log.i("info", "Provider ON");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("LocAndroid", "Provider Status: " + status);
Log.i("info", "Provider Status: " + status);
}
}
And in onCreate () I launch the service:
public class PacienteApp extends Application {
#Override
public void onCreate() {
Intent intent = new Intent(this, GpsService.class);
this.startService(intent);
}
}
Using Broadcast Receiver is a good idea, but laborious as they are not allowed to use handlers within this class. And anyway you need the service to run in the background.

Call LocationListener Service from BroadcastReceiver

I want to call LocationListener Service from BroadcastReceiver, So I can get current location of device and call my API on server after every 5 minutes in backend, even if application is not running.
So, I have created class with BroadcastReceiver, Which will call my LocationListener Service. In this service, I want to get location and save it in db.
The problem is, I am not able to call LocationListener Service from BroadcastReceiver. BroadcastReceiver running well.
Code,
Run BroadcastReceiver in AlarmManager.
public void LocationClicked(View view){
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver class file,
public class LocationReceiver extends BroadcastReceiver {
private static int count = 0;
#Override
public void onReceive(Context context, Intent intent) {
count++;
Toast.makeText(context, "I'm running " + count, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(context, LocationUpdateService.class);
context.startService(myIntent);
}
}
"I'm running " is coming.
LocationListerner Service file,
public class LocationUpdateService extends Service implements LocationListener {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onLocationChanged(Location location) {
double ss = location.getLatitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
Here what do I need to change to make it work ?
And tell me If I am doing it in wrong way.

Location tracing using services in android

i want a background service class that which will get the user current location sends as a notification to the user if the user changes its location then the updated location will sends back to the user as a notification..even if the user close the application then also will receive the notification (the service will trace the location in background even application closed)
currently i am using this service for tracing the location which works if the application in foreground
can u pls. help me to get out of this i am struggling for 4 hours
in your activity unlike the one that you are using make your buttons start and stop the service and it will run in the background unless the user reopens the app and stops it
Intent i = new Intent(MainActivity.this,gpsservice.class);
startService(new Intent(i));
or to stop
Intent j = new Intent(MainActivity.this,gpsservice.class);
stopService(new Intent(j));
use this service
public class gpsservice extends Service{
private LocationManager locationManager;
MyLocationListener locationListenerp;
public gpsservice() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListenerp = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 5000, 10, locationListenerp);
}
#Override
public void onDestroy() {
locationManager.removeUpdates(locationListenerp);
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "location Service Started", Toast.LENGTH_LONG).show();
}
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), "I was here", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String s) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}

Toggle Service via ToggleButton for GPS Tracker

i'm currently writing an app that should to the following:
The UI only contains one toggle button. If it is turned on the GPS Position shall be sent to an external server. If it is turned off, nothing shall happen.
If the Button was turned on and the app (activity) is closed the location should still be send until the Button is turned off again.
How can i achieve this ? I read a lot of threads and tutorials and on dev.google.com, but i was not able to find the best solution for my problem.
My current approach:
MainActivity.java
public void onClick(View v) {
if (onOffButton.isChecked()) {
Intent startIntent = new Intent(this, LocationService.class);
startService(startIntent);
} else {
stopService(new Intent(this, LocationService.class));
}
}
LocationService.java
public class LocationService extends Service implements LocationListener {
final public static String START_ACTION = "START_LOCATION";
final public static int NOTE_ID = 1;
private int updateRate;
private LocationManager locationManager;
private NotificationManager notifyManager;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// show popup message
Toast.makeText(this, getText(R.string.start_message), Toast.LENGTH_SHORT).show();
// display icon in status bar
requestLocationUpdates();
}
private void requestLocationUpdates() {
if(locationManager != null)
locationManager.removeUpdates(this);
// get location service
Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.ACCURACY_FINE);
String bestProvider = getLocationManager().getBestProvider(crit, true);
getLocationManager().requestLocationUpdates(bestProvider, updateRate * 1000,
0 /* minDist */, this);
LocationService.running = true;
}
#Override
public void onDestroy() {
super.onDestroy();
getLocationManager().removeUpdates(this);
notifyManager.cancel(NOTE_ID);
Toast.makeText(this, getText(R.string.stop_message), Toast.LENGTH_SHORT).show();
LocationService.running = false;
}
#Override
public void onLocationChanged(Location location) {
//Send Location to Server
}
#Override
public void onProviderDisabled(String provider) {
// TODO stop service, notify user
}
#Override
public void onProviderEnabled(String provider) {
requestLocationUpdates();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO notify user
}
private LocationManager getLocationManager() {
if (this.locationManager == null)
this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return this.locationManager;
}
}
I got this from a very old gps tracker i found on the Internet (i know that onStart() is deprecated and should replaced with onCommandStart()) I just want to know if the general approach is good..
Regards.
The approach looks fine. You just need to implement the call-back methods to report your location.

LocationManager.removeUpdates(listener) not removing location listener

My app scenario is that I want to track employees location. I have a broadcastreceiver which listens device boot broadcast and register an alarm manager. When alarm manager ticks, it registers two location listeners, one to listen to gps and other for network. I want that when I got first location update in onLocationChange() method, save location and unregister that location listener so that when alarm manager ticks again, it does not get duplicate. To unregister, I have location listeners as static objects to access them in onLocationChange(). But I have found that it is NOT removing location listener. Here is my code example:
public class BootTimeServiceActivator extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent mIntent = new Intent(context, MyBroadCastReceiver.class);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20 * 60 * 1000, mPendingIntent);
}
}
//..........
public class MyBroadCastReceiver extends BroadcastReceiver{
public static LocationManager locationManager;
public static MyLocationListener networkLocationListener;
public static MyLocationListener gpsLocationListener;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm has been called...", Toast.LENGTH_SHORT).show();
initLocationListeners(context);
registerLocationListeners();
}
public void initLocationListeners(Context context) {
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
networkLocationListener = new MyLocationListener(context);
gpsLocationListener = new MyLocationListener(context);
}
public void registerLocationListeners() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, gpsLocationListener);
}
}
\\.....
public class MyLocationListener implements LocationListener {
Context context;
public MyLocationListener(Context context) {
this.context = context;
}
#Override
public void onLocationChanged(Location location) {
if(location != null) {
SDCardService sdService = new SDCardService(context);
try {
sdService.logToDB(location);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("provider",location.getProvider());
if(location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.gpsLocationListener);
}
else if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.networkLocationListener);
}
}
}
Can anyone guide me where I am wrong?
Try this..
lmNetwork.removeUpdates(networkLocationListener);
lmGps.removeUpdates(gpsLocationListener);
Ahh... I have a typo mistake. Actually in MyBroadCastREciever class I was registering gpsListener twice as:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
100, 0,
gpsLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
100, 0,
networkLocationListener); //previously was gpsLocationListener again. Wrong.
that's y network listener was not being removed.
you can do it this way with an insider class and remove the listener onPause() :
#Override
protected void onPause() {
super.onPause();
GPSlocationManager.removeUpdates(GPSLocationListener);
CellLOcationManager.removeUpdates(NetworkLocationListener);
}
private void SetLocationManager() {
GPSlocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
CellLOcationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
NetworkLocationListener = new MyNetworkLocationListener();
GPSLocationListener = new MyGPSLocationListener();
GPSlocationManager.requestLocationUpdates("gps", 5000, 1, GPSLocationListener);
CellLOcationManager.requestLocationUpdates("network", 10000, 1, NetworkLocationListener);
}
private class MyNetworkLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}

Categories

Resources