如何在 stopService 后 运行 做些什么?

How to run something after stopService?

我想在服务停止时接收消息,所以我在服务的 onDestroy 部分添加了一些代码 class。

这是 Main 的代码 Activity

    if(view.getId() == R.id.stopButton) {
        stopService(new Intent(this, MyService.class));
        Toast.makeText(this, "Button Pressed", Toast.LENGTH_SHORT).show();

这是为了服务

public void onDestroy() {
        Toast.makeText(this, "Some Message", Toast.LENGTH_SHORT).show();
    }

我想要实现的是在按下停止按钮时显示 "Some Message" 然后 "Button Pressed" 消息。

从逻辑上讲,应该先显示"Some Message",然后再显示"Button Pressed"消息。 但无论我如何尝试(更改 stopService 的顺序)"Button Pressed" 消息首先出现。

我是不是漏掉了什么?请帮助.. :)

stopService() 是异步的。当您调用 stopService() 时,您会立即取回控制权,稍后将停止服务。