我正在做一个基于gps的应用程序,我使用gps作为我的项目的服务,当gps打开它正确显示位置并且应用程序运行正常,但之后我完全退出我的应用程序和gps关闭它显示崩溃消息。这意味着即使我离开或退出我的应用程序服务类仍在运行,如果我在应用程序运行时关闭gps应用程序崩溃,但如果我没有转动gps它工作好的。现在我该如何解决这个问题。我将展示用于gps的服务类
public class Gps_Service extends Service {
TextView txtv;
List<Address> myList;
String addressStr;
LocationManager lm;
LocationListener ll;
SQLiteDatabase DiaryDB = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("@@@@@@@@@@@ serviceOnCreate");
// Toast.makeText(this, "GPS Service started(Diary)", Toast.LENGTH_LONG).show();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
@Override
public void onDestroy() {
super.onDestroy();
// Toast.makeText(this, "GPS Service Destroyed(Diary)", Toast.LENGTH_LONG).show();
lm.removeUpdates(ll);
System.out.println("@@@@@@@@@@@ inside ONDESTROY GPS listener removed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");
// Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
private class mylocationlistener implements LocationListener
{
public void onLocationChanged(Location location)
{
if (location != null) {
Geocoder myLocation = new Geocoder(Gps_Service.this, Locale.getDefault());
// Toast.makeText(MyService.this,location.getLatitude() + " " + location.getLongitude() +"\n",Toast.LENGTH_LONG).show();
// DiaryDB = MyService.this.openOrCreateDatabase("DIARY_DATABASE", MODE_PRIVATE, null);
// DiaryDB.execSQL("INSERT INTO Locations(LATITUDE, LONGITUDE) VALUES('" +location.getLatitude()+"','"+location.getLongitude()+"')");
// DiaryDB.close();
// ParseTweetsActivity.latitude = ""+location.getLatitude();
//ParseTweetsActivity.longitude = ""+location.getLongitude();
AndroidGoogleMapsActivity.lats = Double.parseDouble(""+location.getLatitude());
AndroidGoogleMapsActivity.longt = Double.parseDouble(""+location.getLongitude());
Advertiser_get_direction.latitudefrom = Double.parseDouble(""+location.getLongitude());
Advertiser_get_direction.longtitudefrom = Double.parseDouble(""+location.getLongitude());
AndroidGoogleMapsActivity.longt = Double.parseDouble(""+location.getLongitude());
System.out.println("saaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+AndroidGoogleMapsActivity.lats);
// ParseTweetsActivity.load();
try
{
myList = myLocation.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if(myList!=null)
{
Address address = (Address) myList.get(0);
addressStr = "";
addressStr += address.getAddressLine(0) + "\n";
addressStr += address.getAddressLine(1) + "\n";
addressStr += "Country name "+address.getCountryName() + "\n";
addressStr += "Locality "+address.getLocality() + "\n";
addressStr += "Country code "+address.getCountryCode() + "\n";
addressStr += "Admin Area "+address.getAdminArea() + "\n";
addressStr += "SubAdmin Area "+address.getSubAdminArea() + "\n";
addressStr += "PostalCode "+address.getPostalCode() + "\n";
// txtv.append(location.getLatitude() + " " + location.getLongitude() +"\n");
// txtv.append(addressStr+"\n");
System.out.println(location.getLatitude() + " " + location.getLongitude() +"\n");
lm.removeUpdates(ll);
}
}
}
public void onProviderDisabled(String provider)
{
// Toast.makeText(MyService.this,"Provider Disabled",Toast.LENGTH_LONG).show();
txtv.setText("");
}
public void onProviderEnabled(String provider)
{
// Toast.makeText(MyService.this,"Provider Enabled.....",Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
答案 0 :(得分:1)
您已在TextView
内宣布Service
。那不好。 TextView
是一个UI组件,并不属于该服务。
在onProviderDisabled()
方法中,您调用txtv.setText("");
,因为txtv
为空,可能会崩溃。这可能是您禁用GPS时应用程序崩溃的原因。
答案 1 :(得分:0)
从
更改您的代码lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
以下
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,ll);
}
答案 2 :(得分:0)
当我没有在onDestroy()中释放监听器时,我遇到了这样的问题。 尝试添加以下代码:
lm.unregisterListener(ll);