2012-06-11 240 views
0

您好我正在做一个基于gps的应用程序,我使用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) 
     { 
     } 
    } 

} 

回答

1

你声明了ServiceTextView我的服务类。这不好。 TextView是一个UI组件,不属于该服务。

在您的onProviderDisabled()方法中,您可以拨打txtv.setText("");,这可能会因为txtv为空而崩溃。这可能是您禁用GPS时您的应用崩溃的原因。

+0

谢谢你r ri8 crash is gone :) – Ramz

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); 
    } 
0

我有一个问题是这样,当我在的onDestroy didn`t释放监听器()。 尝试添加下面的代码:

lm.unregisterListener(ll); 
相关问题