启动GPS服务时强制关闭

时间:2011-09-13 07:00:23

标签: android service gps

启动服务时出现问题:应用程序显示强制关闭消息。

主要活动中的startservice如下所示:

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

GP Service.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 个答案:

答案 0 :(得分:0)

您需要在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">