无法在 android 中使用 Handler 停止服务
Not able to stop service using Handler in android
我刚刚在 Handler.now 中启动后台服务 我想停止服务,但服务没有从 Handler 和任何其他 class 停止。
下面是我的代码:-
下面是我尝试启动和停止服务的处理程序。
mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
Log.e("mTrackStatus",String.valueOf(mTrackStatus));
if (mTrackStatus == 1) {
Log.e("BACKGROUND=", "background Service Start");
//handler will call after every 10 seconds
context.startService(new Intent(context, BackgroundService.class));
mHandler.postDelayed(mRunnable, 10000);
} else if (mTrackStatus == 2) {
Log.d("StopTracking", "StopTracking");
context.stopService(new Intent(context, BackgroundService.class));
mHandler.removeCallbacks(mRunnable);
}
}
};
// mHandler.postDelayed(mRunnable, 10000);
mRunnable.run();
I have tried Lot's Of method like
1. context.stopService(new Intent(context,BackgroundService.class)); and stopSelf(); but it will not work for me
我已经在这个问题上花了 2 天时间。最后我解决了 Issue.Actually 我的 BackgrounService 是停止通过使用 :-
context.stopService(新意图(上下文,BackgroundService.class));
我在我的服务中使用 onLocationChanged() 方法 class,当我尝试停止服务时,服务已停止,但 onLocationChanged() 方法在后台继续 运行。通过使用停止服务服务已停止但 onLocationChanged() 未停止,因为它 Android 我们无法停止的默认方法。
我刚刚在 Handler.now 中启动后台服务 我想停止服务,但服务没有从 Handler 和任何其他 class 停止。 下面是我的代码:- 下面是我尝试启动和停止服务的处理程序。
mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
Log.e("mTrackStatus",String.valueOf(mTrackStatus));
if (mTrackStatus == 1) {
Log.e("BACKGROUND=", "background Service Start");
//handler will call after every 10 seconds
context.startService(new Intent(context, BackgroundService.class));
mHandler.postDelayed(mRunnable, 10000);
} else if (mTrackStatus == 2) {
Log.d("StopTracking", "StopTracking");
context.stopService(new Intent(context, BackgroundService.class));
mHandler.removeCallbacks(mRunnable);
}
}
};
// mHandler.postDelayed(mRunnable, 10000);
mRunnable.run();
I have tried Lot's Of method like 1. context.stopService(new Intent(context,BackgroundService.class)); and stopSelf(); but it will not work for me
我已经在这个问题上花了 2 天时间。最后我解决了 Issue.Actually 我的 BackgrounService 是停止通过使用 :- context.stopService(新意图(上下文,BackgroundService.class));
我在我的服务中使用 onLocationChanged() 方法 class,当我尝试停止服务时,服务已停止,但 onLocationChanged() 方法在后台继续 运行。通过使用停止服务服务已停止但 onLocationChanged() 未停止,因为它 Android 我们无法停止的默认方法。