2012-06-11 205 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); 
相關問題