如何在我的程序中间停止 Intent Service?

How to stop Intent Service in middle of my program?

调用 stopService(Intent) 无效,即使我正在按 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();
            }
            }
        }
    }
}

如何停止 IntentService

Intent 服务在工作完成后自行停止,您无需明确停止它。

你不能那样停止你的 IntentServiceIntentService 自动停止。

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.

您可以在 IntentService 中定义一个 boolean isStop 值,并在单击 stop service 按钮时更改该值。您必须检查 for 循环中的布尔值,例如:

 // 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();
        }
 }

为了获得最佳实践,您应该使用 LocalBroadCastReceiver 而不是静态布尔值。