Can't stop IntentService



  • I've got this kind of counter in my service:

     private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            mCurrentMilis = System.currentTimeMillis();
            mElapsedTime = mCurrentMilis - mLastMilis;
            mLastMilis = mCurrentMilis;
            mTotalMilis += mElapsedTime;
    
        responseResumeClick(mTotalMilis);
        handler.postDelayed(this, 100);
    }
    

    };

    And trying to stop the service by the next way.

    ChronometrService.stop(getApplicationContext());

    public static void stop(Context context){
    Intent intent = new Intent(context, ChronometrService.class);
    context.stopService(intent);
    }

    or

    stopService(new Intent(getApplicationContext(),ChronometrService.class));

    He's still selling slaves. How can you stop him?

    Full service code

    public class ChronometrService extends IntentService {

    public static final String ACTION_RESUME_CLICK = "resumeClick";
    public static final String ACTION_STOP_SERVICE = "stopService";
    public static final String RESPONSE_RESUME_CLICK = "responseResumeClick";
    public static final String EXTRA_PARAM = "EXTRA_PARAM";
    private static final String TAG = "ChronometrService";

    private final Handler handler = new Handler();

    private static long mTotalMilis;
    private long mLastMilis;
    private long mElapsedTime;
    private long mCurrentMilis;

    public ChronometrService() {
    super("ChronometrService");
    }

    public static void resumeClick(Context context, long totalMilis) {
    mTotalMilis = totalMilis;
    Intent intent = new Intent(context, ChronometrService.class);
    intent.setAction(ACTION_RESUME_CLICK);
    context.startService(intent);
    }

    public static void stopService(Context context) {
    Intent intent = new Intent(context, ChronometrService.class);
    intent.setAction(ACTION_STOP_SERVICE);
    context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    if (intent != null) {
    final String action = intent.getAction();
    Log.d(TAG, action);
    switch (action) {
    case ACTION_RESUME_CLICK:
    mLastMilis = mElapsedTime = System.currentTimeMillis();
    sendUpdatesToUI.run();
    break;

            case ACTION_STOP_SERVICE:
                //todo stop method
        }
    
    }
    

    }

    private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
    mCurrentMilis = System.currentTimeMillis();
    mElapsedTime = mCurrentMilis - mLastMilis;
    mLastMilis = mCurrentMilis;
    mTotalMilis += mElapsedTime;

        responseResumeClick(mTotalMilis);
        System.out.println(TAG + " mTotalMilis =  " + mTotalMilis);
        handler.postDelayed(this, 100); // 0.1 seconds
    }
    

    };

    public void responseResumeClick(long totalMilis) {
    Intent intent = new Intent(RESPONSE_RESUME_CLICK);
    intent.putExtra(EXTRA_PARAM, totalMilis);
    LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this);
    bm.sendBroadcast(intent);
    }



  • If you're really done, IntentServiceand he doesn't have to stop, he's usually after he's done. onHandleIntent() It doesn't do anything else, so it stops.

    It's in your code. Handlerhow to stop it, without the knowledge of the whole code, it's almost impossible to say. Usually, it's like,

    private Runnable sendUpdatesToUI = new Runnable() {
      public void run() {
        if (isRunning) {
            mCurrentMilis = System.currentTimeMillis();
            mElapsedTime = mCurrentMilis - mLastMilis;
            mLastMilis = mCurrentMilis;
            mTotalMilis += mElapsedTime;
    
        responseResumeClick(mTotalMilis);
        handler.postDelayed(this, 100);
    }
    

    }
    };

    UPDATE♪ I don't think this approach is ideologically correct because IntentService It's not meant for such tasks, but it's a record.

    private static boolean isRunning;
    .....
    @Override
    protected void onHandleIntent(Intent intent) {
    if (intent != null) {
    final String action = intent.getAction();
    Log.d(TAG, action);
    switch (action) {
    case ACTION_RESUME_CLICK:
    mLastMilis = mElapsedTime = System.currentTimeMillis();
    isRunning = true;
    sendUpdatesToUI.run();
    break;

        case ACTION_STOP_SERVICE:
            isRunning = false;
            break;
    }
    

    }
    }

    Code Runnable It's like the beginning of the answer.

    One nuance. If after STOP Coming. RESUME In a time less than 100ms, there's a possibility that there'll be a second copy. Runnable♪ We need to take care of this. If these events are user-generated, the probability is almost zero.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2