Force close when start GPS service

255 Views Asked by At

I have a problem when the service is started: the appliaction shows a force close message.

my startservice in the main activity looks like this:

startService(new Intent(Main.this,GPSService.class));

GPSService.class

public class GPSService extends Service implements LocationListener {
LocationManager locationManager;
double x1,x2;
@Override
public IBinder onBind(Intent intent)
{
    return null;

}
@Override 
public void onCreate()
{
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);   
}

public void  onLocationChanged(Location loc)
{
    x1=loc.getAltitude();
    x2=loc.getLatitude();
}
public void onProviderEnabled(String s){
    //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
}
public void onProviderDisabled(String s){
    //locationManager.removeUpdates(this);
    //x1=0;
   // x2=0;
}
 public void onStatusChanged(String s, int i, Bundle b){}
 @Override
    public void onDestroy() {       
        locationManager.removeUpdates(this);
    }
}
1

There are 1 best solutions below

0
Dhruvisha On

You need to give following permissions in androidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES">