0

我创建了一个运行活动和服务的 android 应用程序。

我已经在活动的 ondestroy() 函数中停止了服务。

但即使应用程序关闭并调用了 ondestroy(),该进程在设置 -> 应用程序 -> 运行服务中仍然可见。

我应该如何完全关闭它?

我用来启动服务的代码:

// initService is called from onCreate() in MainActivity
public void initService() {
    try {
        serviceConnection = new CallService();
        Intent serviceIntent = new Intent();
        serviceIntent.setClassName("com.example.working",
                com.example.working.MyService.class.getName());
        boolean ret = this.bindService(serviceIntent, serviceConnection,
                Context.BIND_AUTO_CREATE);
        Toast.makeText(MainActivity.this, "Service bound : " + ret,
                Toast.LENGTH_SHORT).show();         // Return true or false
    } catch (Exception e) {
        Toast.makeText(this, "initService(): " + e.toString(), Toast.LENGTH_SHORT)
                .show();
    }
}

class CallService implements ServiceConnection {
    @Override
    public void onServiceConnected(ComponentName name, IBinder boundInterface) {
        try {
            Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_SHORT)
                    .show();
            aidlObject = WorkingAidl.Stub.asInterface((IBinder) boundInterface);
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "onServiceConnected: " + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName paramComponentName) {
        aidlObject = null;
        Toast.makeText(MainActivity.this, "Service Disconnected", Toast.LENGTH_SHORT)
                .show();
    }
}

谢谢

4

0 回答 0