gpt4 book ai didi

android - 不可阻挡的 Android 服务正在监听地理定位

转载 作者:行者123 更新时间:2023-11-29 22:09:30 27 4
gpt4 key购买 nike

我实现了用于监听用户位置的 Android 服务:

public class ListenLocationService extends Service {
IBinder mBinder = new LocalBinder();
public interface ILocationService {
Location userLocation = new Location("");
public void StartListenLocation();
public void StopListenLocation();
public Location getUserLocation();
}
LocationManager locationManager;
LocationListener locationListener;
public class LocalBinder extends Binder implements ILocationService{

public void StopListenLocation(){
//so many attempts to stop service and no one helped
locationManager.removeUpdates(locationListener);
locationListener = null;
stopSelf();
}

public void StartListenLocation()
{
locationManager = (LocationManager)ListenLocationService.this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {

public void onStatusChanged(String provider, int status, Bundle extras) {
}

public void onProviderEnabled(String provider) {
}

public void onProviderDisabled(String provider) {
}

public void onLocationChanged(Location location) {
userLocation.set(location);
}
};
//if I have only one requestLocationUpdates situation is the same

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
400, 1, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
400, 1, locationListener);
}

public Location getUserLocation(){
return userLocation;
}
}

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

}

我在两个 Activity 中绑定(bind)到此服务。在第一个 Activity 中,我只是启动服务:

    private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
mService = (ILocationService) service;
mService.StartListenLocation();
}

public void onServiceDisconnected(ComponentName arg0) {
Log.d("LOG","onServiceDisconnected");
}
};

public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
@Override
public void onStop(){
super.onStop();
unbindService(mConnection);
mService.StopListenLocation();
}

如果我仅在我的应用程序中启动第一个 Activity,然后关闭它,则服务停止 - GPS 标记从设备屏幕上消失。

但是,如果我进入第二个 Activity (我在其中获取 userLocation)并在此之后关闭两个 Activity (使用 Android 后退按钮),那么 GPS 标记仍然在我的屏幕上!

第二个 Activity 的代码:

    private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
mService = (ILocationService) service;
userLocation = mService.getUserLocation();
}

public void onServiceDisconnected(ComponentName arg0) {
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
@Override
public void onStop(){
super.onStop();
unbindService(mConnection);
mService.StopListenLocation();

}

你可以看到我在 onStop() 方法中不仅写了 unbindService (最初我认为这就足够了)而且我正在调用 StopListenLocation 函数与此代码:

locationManager.removeUpdates(locationListener);
locationListener = null;
stopSelf();

如果只有第一个 Activity 绑定(bind)到它,它会停止服务。但是,如果两个 Activity 都已绑定(bind),则服务不会在 onStop() 之后停止。我使用 Log.d 确保调用了所有方法:onStop()、stopListenLocation()、onBind() 等。唯一的问题是 onServiceDisconnected () 从未被调用过。

我想情况是我的服务启动了另一个用于监听位置的系统服务。我停止了我的服务,但尽管 locationManager.removeUpdates(locationListener);,这个控制 GPS 的系统服务继续工作。也许我的建议有误。

如何在两个 Activity 都停止的情况下停止 GPS?

最佳答案

BIND_AUTO_CREATE 并不一定意味着服务会在没有连接时自行终止,它只是意味着如果系统需要资源,它是斩波器上的第一件事。如果系统没有资源匮乏,它会让你的服务继续运行,因为如果你的 Activity 再次需要服务,绑定(bind)到现有实例比从头开始创建它在时间上更省钱。

简而言之,它会继续“运行”,但不用担心 - 它不会消耗其他地方需要的任何东西。

编辑:另请注意,仅仅因为您已与服务解除绑定(bind)并不意味着它将中断服务正在异步处理的任何正在运行的处理任务。所发生的一切只是您告诉操作系统您不再对结果感兴趣。您必须在断开与服务的连接之前明确停止任何正在运行的任务,否则该任务将继续消耗 CPU 时间

关于android - 不可阻挡的 Android 服务正在监听地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com