【问题标题】:Android app crashes onDestroy while a location service is runningAndroid 应用程序在运行位置服务时崩溃 onDestroy
【发布时间】:2017-02-27 14:17:35
【问题描述】:

我编写了一个启动定位服务的应用程序,该服务使用 Intent 获取地址,并在该位置位于预定义半径之内和之外时弹出一个通知。 问题是当我试图破坏 MainAvtivity 时。我希望服务保持活动状态,但是当我销毁应用程序时,它会崩溃。

我的代码:

LocationService.java

public class LocationService extends Service {

private LocationManager mLocationManager;
private boolean inRange = false;
private boolean isNotificationPosted = false;
private DatabaseHelper db;
private ReportItem item;
private float lastKnownDistance;
private Location chosenLocation;

private static final long LOCATION_REFRESH_TIME = 10000;
private static final float LOCATION_REFRESH_DISTANCE = 10;
private static final int RADIUS = 100;
private static final float INITIAL_DISTANCE = 1000;

private LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location)
    {
        float distance = location.distanceTo(chosenLocation);

        if(!isNotificationPosted)
        {
            if (lastKnownDistance > distance) {

                //Entering workplace radius
                if (distance < RADIUS && !inRange) {
                    Log.i("Service", "in range with notification");

                    inRange = true;
                    item = new ReportItem();
                    makeNotification();
                    isNotificationPosted = true;

                }
            }
        }

        else
        {
            if (lastKnownDistance < distance) {

                //Leaving workplace radius
                if (distance > RADIUS && inRange) { //out of range
                    Log.i("Service", "out of range with notification");

                    inRange = false;
                    item.setExit(new Date());
                    db.createReport(item);
                    makeNotification();
                    isNotificationPosted = false;
                }
            }
        }

        lastKnownDistance = distance;
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText( getApplicationContext(), "Please turn on your location services", Toast.LENGTH_SHORT ).show();
    }
};

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(final Intent intent, int flags, int startId)
{
    mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    final Address chosenAddress = intent.getParcelableExtra("chosen address");
    chosenLocation = setLatLong(chosenAddress.getLongitude(), chosenAddress.getLatitude());
    lastKnownDistance = INITIAL_DISTANCE;
    Log.i("Service", "lase known location, distance: " + Float.toString(lastKnownDistance));

    db = new DatabaseHelper(this);

    ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    mLocationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            LOCATION_REFRESH_TIME,
            LOCATION_REFRESH_DISTANCE,
            mLocationListener
    );

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    db.close();
    ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    this.mLocationManager.removeUpdates(this.mLocationListener);

}

从 MainActivity 启动服务:

serviceIntent = new Intent(MainActivity.this, LocationService.class);
//selected address is from type Address
serviceIntent.putExtra("chosen address", selectedAddress);
saveIntentIntoSharedPreferences(serviceIntent);
startService(serviceIntent);

知道可能导致问题的原因吗?

谢谢!

【问题讨论】:

  • 发布错误日志!

标签: java android service location main-activity


【解决方案1】:

这个解决方案对我有用:

我从传递给服务的 Intent 中删除了 Address 对象,它突然完美地工作了。 我从 Service 中的 sharedPreferences 中获得了我的 Address 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多