I’m trying to stop it with stopSelf() or stopService() methods. But still it’s running and executing actions. So how to stop executing this?
It can’t be stopped. It is designed to run all time, so you can’t call onDestroy that easily. You can either cancel it completely, using the disableSelf()
method, but then the user will have to enable it manually again. I’ll show you how to do it another way.
Send STOP_SERVICE
via BroadcastReceiver
and then globally set the boolean variable mIsStopServiceRequested
. In places where the service is programmed to perform certain actions check if there was a request to stop, if so, just don’t perform the action. This is the only way it can be stopped, Thanks
sendBroadcast(new Intent(AppConstants.STOP_ACCESSIBILITY_SERVICE));
public class MyAccessibilityService extends AccessibilityService {
private BroadcastReceiver mReceiver;
private boolean mIsStopServiceRequested = false;
@Override
protected void onServiceConnected() {
super.onServiceConnected();
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (AppConstants.STOP_ACCESSIBILITY_SERVICE.equals(intent.getAction())) {
mIsStopServiceRequested = true;
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(AppConstants.STOP_ACCESSIBILITY_SERVICE);
registerReceiver(mReceiver, filter);
}