gpt4 book ai didi

java - 调用 stopService() 时 Android 服务不会停止

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:03 26 4
gpt4 key购买 nike

我在停止已启动的服务时遇到问题。

该服务在用户登录时被调用,并开始跟踪用户的位置。这工作正常。当用户按下注销按钮并成功注销时,该服务将停止。

这是一个由 HTML 按钮通过 JavaScript 接口(interface)调用的 android 服务。

这是我的主类,其中包含启动和停止服务的方法:

public class CasesMain extends DroidGap {
Intent gpsTracking;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();

appView.addJavascriptInterface(this, "StartGPS");

super.loadUrl(Config.getStartUrl());
}
public void startGPS(){
gpsTracking = new Intent(this, MyService.class);
startService(gpsTracking);
}

public void stopGPS(){
stopService( gpsTracking );
}

这是 MyService 类:

public class MyService extends Service {
private LocationManager locManager;
private LocationListener locListener = new myLocationListener();

private boolean gps_enabled = false;
private boolean network_enabled = false;

private Handler handler = new Handler();
Thread t;

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

@Override
public void onCreate() {
}

@Override
public void onDestroy() {
}

@Override
public void onStart(Intent intent, int startid) {
}

public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
.show();

final Runnable r = new Runnable() {
public void run() {
location();
handler.postDelayed(this, 10000);
}
};
handler.postDelayed(r, 10000);
return START_STICKY;
}

public void location() {
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

try {
gps_enabled = locManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locListener);
}
}

private class myLocationListener implements LocationListener {
double lat_old = 0.0;
double lon_old = 0.0;
double lat_new;
double lon_new;

@Override
public void onLocationChanged(Location location) {

if (location != null) {
locManager.removeUpdates(locListener);
lon_new = location.getLongitude();
lat_new = location.getLatitude();
String longitude = "Longitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
Log.v("Debug", "Latt: " + latitude + " Long: " + longitude);
Toast.makeText(getApplicationContext(),
longitude + "\n" + latitude, Toast.LENGTH_SHORT).show();
lat_old = lat_new;
lon_old = lon_new;
}
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

这是我停止服务的调用:window.StartGPS.stopGPS();

开始工作完美,但当我注销时,应用程序继续显示 latt 和 long 的消息,这意味着对 stopService() 的调用要么没有工作,要么没有被调用。

谁能看出我的错误在哪里或出了什么问题?

任何帮助将不胜感激!

固定MyServiceonDestroy() 方法中添加 handler.removeCallbacksAndMessages(null); 为我修复了它。

最佳答案

Can anyone see where my mistake is or what is going wrong?

您有一个空的 onDestroy() 方法。您需要在您的 Handler 上调用 removeCallbacks()

关于java - 调用 stopService() 时 Android 服务不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20907670/

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