【问题标题】:how to stop a Location listener service? (not stopping now)如何停止位置侦听器服务? (现在不停止)
【发布时间】:2017-05-02 06:10:53
【问题描述】:

我正在使用位置侦听器服务每隔几秒更新一次用户的当前位置。我还使用了 WakeLock 让监听器在后台工作。现在一切正常,但我无法停止 Location 监听器。即使应用程序被强制关闭,侦听器也会将用户位置更新到数据库。

位置监听器:

 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.location.Location;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;

public class MyLocationService extends Service {
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 3000;
private static final float LOCATION_DISTANCE = 0f;
Context context = this;
PowerManager.WakeLock wakeLock;
public int e=0;

LocationListener[] mLocationListeners = new LocationListener[]{
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    Toast.makeText(this, "MyService Started.", Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

@Override
public void onCreate() {
    Log.e(TAG, "onCreate");
    initializeLocationManager();
    PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    wakeLock= mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    }
}

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    wakeLock.release();
    super.onDestroy();


    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}

// create LocationListener class to get location updates
private class LocationListener implements android.location.LocationListener
{
    Location mLastLocation;
    double latitude;
    double longitude;
    LatLng latLngcurrent;


    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Store.latu=latitude;
        Store.longu=longitude;
        latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
        Toast.makeText(context,"Location " + String.valueOf(e), Toast.LENGTH_SHORT).show();
        e++;
        Toast.makeText(MyLocationService.this,Store.latu+" "+Store.longu, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}
}

启动服务意图:(来自 mainactivity)

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);


    Intent serviceIntent  = new Intent(context , MyLocationService.class);
    context.startService(serviceIntent  );



    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }
    if (!isGooglePlayServicesAvailable()) {
        finish();
    } else {
        Log.d("onCreate", "Google Play Services available.");
    }
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

停止意图:(来自 mainactivity)

 private class LogOutTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        Intent i = new Intent(MapsActivity.this, Login.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        Intent intent = new Intent(MapsActivity.this, MyLocationService.class);
        stopService(intent);
        finish();
    }
}

停止意图在用户注销时被调用,现在服务类中的 onDestroy() 方法被调用但它并没有停止进程。当应用程序被强制关闭时,甚至不会调用 onDestroy()。在这两种情况下,流程都会恢复并继续将值更新到 DB。提前致谢。

【问题讨论】:

  • removeUpdates() 被调用了吗?或者由于权限IF而返回该方法?但是,为什么在删除侦听器时要检查权限?这应该在initializeLocationManager() 内或附近的某个地方
  • 我没有检查 removeupdates() 是否被调用。我添加了权限,因为 android studio 向我推荐。 @亚赞
  • 我怀疑这个地方是否适合请求许可(同时破坏服务?!)但是,将它放在循环中并不是很好,你可以请求一次(在循环之前) 还尝试在 removeUpdates() 之前添加一个日志,类似于 Log.i(TAG, "removing listeners ..."); 并查看它是否出现在 logcat 中
  • 我已经删除了权限检查并添加了 log(),现在当我注销时,日志值正在打印,然后服务停止。但是当应用程序从后台强制关闭时,服务不会被终止,并且 onDestroy 也不会被调用。 @亚赞
  • 好的,这是另一个问题,我建议您单独问,但我认为它与return START_STICKY; 有关此主题需要阅读,以便您了解服务在应用程序终止、重新启动时的行为。 ..等

标签: android intentservice wakelock locationlistener android-wake-lock


【解决方案1】:

解决方案: 我终于找到了解决方案。正如@Yazan 所说,我已经从 onDestroy() 方法中删除了权限,这有助于在用户注销时手动停止服务。并在应用程序被强制关闭时停止服务,在您的清单中同时使用标签添加这一行。

片段:

 <service android:name=".MyLocationService"
 android:stopWithTask="true"></service>

当我们强制关闭应用程序时,stopWithTask 将停止服务,并且 LocationListener 也将停止。

here 是文档。添加后,我的应用程序工作正常。

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多