Android 我的应用程序在gps关闭时崩溃了?

Android 我的应用程序在gps关闭时崩溃了?,android,service,gps,Android,Service,Gps,您好,我正在做一个基于gps的应用程序,我使用gps作为我项目的服务,当gps打开时,它会正确显示位置,应用程序运行良好,但之后我完全退出我的应用程序,当gps关闭时,它会显示崩溃消息。这意味着即使我离开或退出我的应用程序,服务类仍在运行,如果我在应用程序运行时关闭gps,应用程序也会崩溃,但如果我没有打开gps,它会正常工作。现在我该如何解决这个问题。我将展示我的gps服务等级 public class Gps_Service extends Service { TextView txtv;

您好,我正在做一个基于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)
        {
        }
   }

}
公共类Gps\u服务扩展服务{
TextView-txtv;
列出我的清单;
字符串地址str;
位置经理lm;
地点:L;
SQLiteDatabase DiaryDB=null;
@凌驾
公共IBinder onBind(意图arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
System.out.println(“serviceocreate”);
//Toast.makeText(这是“GPS服务启动(日志)”,Toast.LENGTH_LONG.show();
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
ll=新的mylocationlistener();
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,ll);
}
@凌驾
公共空间{
super.ondestory();
//Toast.makeText(这是“GPS服务被破坏(日记)”,Toast.LENGTH_LONG.show();
lm.删除更新(ll);
System.out.println(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId)
{
Log.v(“StartServiceAtBoot”、“StartBootService--onStartCommand()”;
//Toast.makeText(此“服务已启动”,Toast.LENGTH_LONG).show();
//我们希望此服务继续运行,直到显式恢复
//停止,所以返回粘性。
返回开始时间;
}
私有类mylocationlistener实现LocationListener
{
已更改位置上的公共无效(位置)
{
如果(位置!=null){
Geocoder myLocation=新的地理编码器(Gps_Service.this,Locale.getDefault());
//Toast.makeText(MyService.this,location.getLatitude()+“”+location.getLatitude()+“\n”,Toast.LENGTH\u LONG.show();
//DiaryDB=MyService.this.openOrCreateDatabase(“日记数据库”,MODE\u PRIVATE,null);
//execSQL(“插入位置(纬度、经度)值(‘“+location.getLatitude()+“,‘“+location.getLatitude()+”)”);
//DiaryDB.close();
//ParseTweetsActivity.latitude=”“+位置。getLatitude();
//ParseTweetsActivity.longitude=”“+location.getLongitude();
AndroidGoogleMapsActivity.lats=Double.parseDouble(“+location.getLatitude());
AndroidGoogleMapsActivity.lont=Double.parseDouble(“+location.getLength());
广告商_get_direction.latitudefrom=Double.parseDouble(“+location.getLongitude());
广告商_get_direction.longtudityfrom=Double.parseDouble(“+location.getLongitude());
AndroidGoogleMapsActivity.lont=Double.parseDouble(“+location.getLength());
System.out.println(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
//ParseTweetsActivity.load();
尝试
{
myList=myLocation.getFromLocation(location.getLatitude(),location.getLatitude(),1);
}捕获(IOE异常){
e、 printStackTrace();
}
如果(myList!=null)
{
地址=(地址)myList.get(0);
addressStr=“”;
addressStr+=address.getAddressLine(0)+“\n”;
addressStr+=address.getAddressLine(1)+“\n”;
addressStr+=“国家名称”+地址.getCountryName()+“\n”;
addressStr++=“Location”+address.GetLocation()+“\n”;
addressStr+=“国家代码”+地址.getCountryCode()+“\n”;
addressStr++=“管理区域”+地址.getAdminArea()+“\n”;
addressStr+=“子管理员区域”+地址。getSubAdminArea()+“\n”;
addressStr++=“PostalCode”+地址。getPostalCode()+“\n”;
//append(location.getLatitude()+“”+location.getLatitude()+“\n”);
//txtv.append(addressStr+“\n”);
System.out.println(location.getLatitude()+“”+location.getLatitude()+“\n”);
lm.删除更新(ll);
}
}
}
公共无效onProviderDisabled(字符串提供程序)
{
//Toast.makeText(MyService.this,“Provider Disabled”,Toast.LENGTH_LONG.show();
txtv.setText(“”);
}
公共无效onProviderEnabled(字符串提供程序)
{
//Toast.makeText(MyService.this,“已启用提供程序…”,Toast.LENGTH_LONG.show();
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra)
{
}
}
}

您已在
服务
中声明了
文本视图
。那不好
TextView
是一个UI组件,不属于该服务

onProviderDisabled()
方法中,调用
txtv.setText(“”)
可能会崩溃,因为
txtv
为空。这可能就是当您禁用GPS时应用程序崩溃的原因。

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

当我没有在onDestroy()中释放侦听器时,我遇到了这样的问题。 尝试添加以下代码:

lm.unregisterListener(ll);