[英]GPS service still running after using stopService()
My application got a background service that handles location updates at a certain interval of time. 我的应用程序有一个后台服务,该服务在一定时间间隔内处理位置更新。 I'm trying to stop the service to start a new one with a different interval but what ends up happening is that the application sends up two location updates at different intervals.
我试图停止该服务以不同的间隔启动一个新服务,但是最终发生的是该应用程序以不同的间隔发送了两个位置更新。 Basically, my first service doesn't stop after using stopService().
基本上,使用stopService()后,我的第一个服务不会停止。
Here's the code that starts the service: 这是启动服务的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity_f);
tiempoGPS = 25000;
startService(new Intent(getBaseContext(), ServicioDeFondo.class).putExtra("TiempoGPS", Integer.toString(tiempoGPS)));
}
Here's the part where I stop it after clicking a button: 这是单击按钮后我将其停止的部分:
private void selectItem(int position) {
...
if(menus[position].equals("Configuracion"))
{
try {
stopService(new Intent(getBaseContext(),ServicioDeFondo.class));
}
catch(Exception e)
{
...
}
}
}
Here my Service class onCreate method: 这是我的Service类的onCreate方法:
@Override
public void onCreate()
{
mHandler = new Handler();
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telefonoID = telephonyManager.getDeviceId();
telephonyManager = null;
createLocationRequest();
buildGoogleApiClient();
mGoogleApiClient.connect();
}
OnstartCommand method: OnstartCommand方法:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
tiempoEntreActualizacion = Integer.parseInt((String) intent.getExtras().get("TiempoGPS"));
reportarGPS = new Thread(new Runnable() { public void run()
{
try
{
while(true)
{
try {
Thread.sleep(tiempoEntreActualizacion);
new APISendClass().execute();
} catch (InterruptedException e) {
e.printStackTrace();
mGoogleApiClient.disconnect();
Thread.currentThread().interrupt();
return;
}
}
}
catch (Exception e)
{
//TODO Auto-generated catch block
e.printStackTrace();
}
} });
reportarGPS.start();
return START_NOT_STICKY;
}
Here's my service class onDestroy method: 这是我的服务类onDestroy方法:
@Override
public void onDestroy(){
reportarGPS.interrupt();
reportarGPS = null;
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
mGoogleApiClient.disconnect();
mGoogleApiClient = null;
mHandler.removeCallbacksAndMessages(null);
Thread.currentThread().interrupt();
super.onDestroy();
}
And last but not least, the methods where I start the locationUpdates: 最后但并非最不重要的一点是,我启动locationUpdates的方法:
@Override
public void onConnected(Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
latitude =String.valueOf(mLastLocation.getLatitude());
longitude = String.valueOf(mLastLocation.getLongitude());
}
startLocationUpdates();
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
Been on stuck on this for 2 days now. 现在已经坚持了2天。 I'm sure its some very basic mistake but I just can't point it out, my onDestroy method is been called but even after running it, the location is still being updated.
我确定它是一些非常基本的错误,但是我无法指出,我的onDestroy方法已被调用,但是即使在运行它之后,该位置仍在更新中。 Why??
为什么?? How do I completely kill the service?
如何完全取消服务?
Your thread is wrong. 您的线程是错误的。 Its never chaecking if its been interrupted.
如果它被打断,它永远不会发出烦恼。 Interrupting a thread does not stop it, it sets a flag that can be checked by the thread itself to see if its done.
中断线程不会停止它,它会设置一个可由线程本身检查以查看其是否完成的标志。 (For a variety of reasons, stopping a thread from anywhere but itself is a really bad idea).
(出于各种原因,在任何地方都停止线程是一个非常糟糕的主意)。 Make your while loop in the thread check isInterrupted and return from its run function if true.
使您的while循环在线程中检查isInterrupted,如果为true,则从其run函数返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.