R.H
R.H

Reputation: 318

How to stop Intent Service in middle of my program?

Calling stopService(Intent) does not work, even though I am pressing mStopButton.

MainActivity

public class MainActivity extends AppCompatActivity {

    MyReceiver myReceiver = new MyReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(myReceiver, new IntentFilter("Start"));

        Button mStartButton = (Button) findViewById(R.id.start_button);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MyIntentService.class);
                startService(intent);
            }
        });

        Button mStopButton = (Button) findViewById(R.id.stop_button);
        mStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MyIntentService.class);
                intent.setAction("stop");
                stopService(intent);
            }
        });
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int res = intent.getIntExtra("count", -1);
            Toast.makeText(MainActivity.this,"Count is " + res,Toast.LENGTH_SHORT).show();
        }
    }
}

MyIntentService

public class MyIntentService extends IntentService {

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

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            for (int i=0; i<=100; i++){
                Intent intent1 = new Intent();
                intent1.setAction("Start");
                intent1.putExtra("count",i);
                sendBroadcast(intent1);
                Log.v("abc",""+i);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
        }
    }
}

How can I stop the IntentService?

Upvotes: 1

Views: 689

Answers (2)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

You cant stop your IntentService that way. IntentService is stopped itself automatically.

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

You can define a boolean isStop value in your IntentService and change the value when stop service button is clicked. You have to check the boolean in the for loop like:

 // when click button
 YourIntentService.isStop = true;    
 // in for loop 
 for (int i=0; i<=100; i++){
            if(isStop) break;
            Intent intent1 = new Intent();
            intent1.setAction("Start");
            intent1.putExtra("count",i);
            sendBroadcast(intent1);
            Log.v("abc",""+i);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 }

For best pratice, you should use LocalBroadCastReceiver instead of static boolean value.

Upvotes: 3

Ashish
Ashish

Reputation: 183

Intent service stops by itself when work is done you don't need to explicitly stop it.

Upvotes: 0

Related Questions