Android GPS not working anymore - android

I was playing around with the GPS on my HTC Desire S and made a very small map application. It was working very well, until I stumbled upon this app.
I uninstalled it again and now my GPS is not working anymore. I know that there is the fix-time, but
locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
ALWAYS returns true, and the app isn't requesting location-updates anymore.
GPSMapTrackerService.java:
package net.hobbycoder.android.gpsmap;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class GPSMapTrackerService extends Service implements LocationListener {
private Resources res;
private FileManager fileManager;
private LocationManager locManager;
private boolean showNotification = true;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
res = getResources();
fileManager = new FileManager(getApplicationContext());
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//GPS on?
if(!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, res.getString(R.string.noGPSText), Toast.LENGTH_LONG).show();
showNotification = false;
stopSelf();
}
else{
showNotification = true;
}
}
#Override
public void onStart(Intent intent, int startID) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(showNotification)
Toast.makeText(this, res.getString(R.string.startedText), Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
locManager.removeUpdates(this);
if(showNotification)
Toast.makeText(this, res.getString(R.string.stoppedText), Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location loc) {
fileManager.write(loc.getLatitude() + ":" + loc.getLongitude() + ";");
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
It seems as if the location got stuck, because the text under my clock widget is saying I am in New York, but I am in Germany.
This app is also not working, so the problem shouldn't be in my code.
Hope anyone can help :(

Service.onStart() is deprecated and if you read the preferred Service.onStartCommand() documentation you'll see that depending on your API onStart() might not even be called.
Try changing onStart() to onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(showNotification)
Toast.makeText(this, res.getString(R.string.startedText), Toast.LENGTH_SHORT).show();
}
Also you never call the super function in any of your overridden methods. Update all of them (except onBind() which does nothing):
#Override
public void onCreate() {
super.onCreate();
...
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(showNotification)
Toast.makeText(this, res.getString(R.string.startedText), Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
// etc, etc

Try using the GPS Status & Toobox app to clear the GPS data and redownload it. I don't have the app currently, but it's somewhere in the menu options.

Related

What is the difference Between gettting location from LocationManager and LocationRequest

When ever I search for getting location I got result to get Location via Location Manager but in a tutorial I have seen that they use LocationRequest to get user Location and I have managed to create a Service that give location based on time
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.model.LatLng;
public class LocationTrackerService extends Service implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
private LocationClient nLocationClient;
public static boolean serviceStopCheck = true;
private DBHelper nDbHelper;
private int timeForService = 60000;
#Override
public IBinder onBind(Intent intent) {
return null;
}
// #Override
// public int onStartCommand(Intent intent, int flags, int startId) {
// timeForService=Integer.valueOf(intent.getStringExtra("TIME_FOR_SERVICE"))*1000;
// return super.onStartCommand(intent, flags, startId);
// }
#Override
public void onDestroy() {
super.onDestroy();
serviceStopCheck = true;
}
#Override
public void onCreate() {
super.onCreate();
nDbHelper = new DBHelper(this);
nLocationClient = new LocationClient(this, this, this);
nLocationClient.connect();
serviceStopCheck = false;
}
#Override
public void onConnectionFailed(ConnectionResult arg0) {
}
#Override
public void onConnected(Bundle arg0) {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(timeForService);
request.setFastestInterval(timeForService);
nLocationClient.requestLocationUpdates(request, this);
}
#Override
public void onDisconnected() {
}
#Override
public void onLocationChanged(Location arg0) {
if (!serviceStopCheck) {
LocationItem nLocationItem = new LocationItem(new LatLng(
arg0.getLatitude(), arg0.getLongitude()),
CommonMethods.getCurentTime(),
CommonMethods.getCurentDate());
try {
nDbHelper.insertLocationItem(nLocationItem);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
stopSelf();
}
}
}
So Is better or I have to use LocationManger. Because I think this code is two much easy then using LocationMnager or GPSTracker class provider by some one.
As for me, I have tested that both methods, and I have a conclusion: LocationManager is better, because it uses hardware GPS and it have better accuracy than LocationClient. LocationClient is a part of GooglePlay services, so I think they are using internet coordinates instead of GPS coordinates. But for me the best way is LocationManager, cause it was giving me the best accuracy.

Android - Update Location every x minutes

I'm pretty new to android, and this is my first post here, so please be kind! :-)
I'm trying to create a service which runs in the background and does a location update every x minutes. To run it every x minutes I'm using the AlarmManager, as described here: Alarm Manager Example
Here's what I've got:
package com.example.service1;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
public class Scheduler extends BroadcastReceiver{
LocationManager locationManager;
LocationListener locationListener;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
//Code which is executed every X seconds/minutes
getLocation(context);
//End of Code
wl.release();
}
public void setScheduler(Context context) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Scheduler.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 20, pi);
}
//Method to get the Location
public void getLocation(Context context) {
Log.e("null","getLocation");
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e(null, "location change");
makeUseOfLocation(location);
}
#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
}
};
}
//Method to work with the location Data; Instance of Point is created
public void makeUseOfLocation(Location location) {
Log.e(null,"makeuse");
Log.e(null,location.getLatitude() + "");
}
}
getLocation() is called every 20 seconds, but then it never runs onLocationChanged() (I use the EmulatorControl in Eclipse to change the location).
I had the same problem before when I used the ScheduledExecutorService instead of the AlarmManager.
Can anyone help me?
Looking at your code I never see the call to locationManager.requestLocationUpdates() with your listener as an argument.
This means that your listener is never registered with the location manager, and therefore does not get called.
You probably only want to register one listener, instead of registering a new one every time.
Try this..
protected void updateNotification() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, normallocationwait, 0.250f, locationListener);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
private class MyLocationlistener implements LocationListener {
public void onLocationChanged(Location location){
if(location!=null){
if(location.hasAccuracy()){
dumpLocation(location);
}else{
dumpLocation(location);
}
}
}
public void onProviderDisabled(String provider){
Log.v("Loc Update","\nProvider disabled: " + provider);
}
public void onProviderEnabled(String provider){
Log.v("Loc Update","\nProvider enabled: " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras){
Log.v("Loc Update","\nProvider status changed: " + provider + ", status="
+ status + ", extras=" + extras);
}

Service does not work?

I had service that run by BrodcastReceiver which suppose to run and give me the GPS data that I requested and I stopped it after I did what suppose to be done, but I notice that It did not provide me with the data that requested and did not stop too , any body can help me in figuring out my problem
the code is below and the service tested in real device:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class GpsService extends Service implements LocationListener{
static LocationManager mlocManager;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
}
#Override
public void onDestroy() {
}
#Override
public void onStart(Intent intent, int startid) {
mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0, 0,this);
}
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
// Here to stop the service and make it finish its task
SystemClock.sleep(40000);
// stop the Gps system by this application
mlocManager.removeUpdates(this);
//Here to stop the service by itself
stopSelf();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
do you have the correct permissions on the manifest?
why do you make the service sleep and for so long? the service works on the UI thread , and such a thing can cause the service to be killed automatically by the system after about 5 seconds because the ui thread doesn't respond.
if you wish to delay the stopping and/or showing of the toast , either use Handler (and postDelayed) , or use asyncTask , or something of your own .
do you have the gps turned on? have you considered using a fake location app or the emulator for this task ?
good luck .

Location listener in a service sends notification issue

What i am trying to do is send a notification from within a service whenever the location is changed. When the user taps on the notification i want the notification to close and start an activity. I managed to send the notification but when the notification is tapped it doesn't clear nor it starts the activity. I can't see where i am going wrong with this!? Here is the code:
package com.oxinos.android.moc;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;
public class mocService extends Service implements OnClickListener{
NotificationManager nm;
static final int uniqueID1= 190910;
PendingIntent pi;
Context con;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
con = getApplicationContext();
Intent intent = new Intent(this, mocActivity2.class);
pi = PendingIntent.getService(this, 0, intent, 0);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
String locStr = "New loc: "+location.getLatitude()+","+location.getLongitude();
String title = "New MOC notification";
Notification startupNotification = new Notification(R.drawable.ic_launcher, locStr, System.currentTimeMillis());
startupNotification.setLatestEventInfo(con, title,locStr, pi);
startupNotification.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID1, startupNotification);
//nm.cancel(uniqueID1);
}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100000, 50, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100000 , 50, locationListener);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(this, mocActivity2.class));
nm.cancel(uniqueID1);
}
}
Any ideas??? What am i doing wrong?
You are creating your PendingIntent using getService(). If you want to start an activity with the PendingIntent, you need to use getActivity().

Android get GPS Location after phone boots, I am trying to get it in a service but location returned as null

Here is the code.
How can I be sure, that location is returned alwasys instead of null.
package com.test.location;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
public class NotifyOnBoot extends Service {
Location location;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
updateNotification();
Thread th = new Thread(null, task, "myService");
th.start();
}
private Runnable task = new Runnable() {
public void run() {
if(location == null)
{
Log.d("location","No location");
}
else
Log.d("location",location.toString());
NotifyOnBoot.this.stopSelf();
}
};
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand (Intent intent, int flags, int startId){
return START_STICKY;
}
protected void updateNotification()
{
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
private class MyLocationlistener implements LocationListener{
public void onLocationChanged(Location location){}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
}
I recommend to create a call back interface and use onLocationChanged which notifies that call back on the moment the location initiate for the first time.
LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

Categories

Resources