Android studio 在主页面的OnDestroy中解绑service关于资源回收的问题

问题遇到的现象和发生背景

我在MainActivity中通过bindService方式绑定了一个服务,并在服务的onbind回调中启动socket对象,在服务的onDestroy中销毁socket,当我在MainActivity的onDestroy中解绑服务,总是不能正常打印socket被销毁的log。而我在主界面中创建按钮然后在按钮点击响应中unbindService,socket每次都能正常显示销毁的log,那么问题来了,在MainActivity的onDestroy中解绑服务是否能触发服务的onbind和onDestroy呢?debug模式下也进不去MainActivity的onDestroy,每次在手机后台划掉APP就直接没输出了。

问题相关代码,请勿粘贴截图

主页面绑定服务

 Intent myServiceIntent;
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
  myServiceIntent = new Intent(MainActivity.this, MyService.class);
        bindService(myServiceIntent, coon, Context.BIND_AUTO_CREATE);
}
//销毁时解绑
protected void onDestroy()
    {
        super.onDestroy();
        if (BuildConfig.DEBUG) Log.d(TAG, "APP销毁");
        KeepManager.getInstance().unregisterKeep(this);
        //页面销毁时解绑
        if(isBind)
        {
            if (BuildConfig.DEBUG) Log.d(TAG, "服务要解绑了啊");
            unbindService(coon);
            isBind=false;
        }
}
//通过按钮解绑
 public void myButtonClick(@NonNull View v)
{
 if(isBind)
        {
            if (BuildConfig.DEBUG) Log.d(TAG, "服务要解绑了啊");
            unbindService(coon);
            isBind=false;
        }
}

服务端代码

 @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        //开启TCP线程
        try {
            //从配置文件获取服务器地址和端口号
            String s_ServerIP= ReadPropertyUtil.getProperty(this,"serverip");
            int i_Port = Integer.parseInt( ReadPropertyUtil.getProperty(this,"serverport"));
            xx = TcpClient.getInstance(s_ServerIP, i_Port, RecvHandler);
            new Thread(xx).start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return iBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
        iBinder = new MyBinder();

        InitAlarm();

        //初始化消息处理器,处理接收到的的上位机信息
        initHandler();

    }

    @Override
    public void onDestroy() {

        Log.d(TAG, "onDestroy: ");
        xx.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onTaskRemoved(Intent rootIntent)
    {
        if (BuildConfig.DEBUG) Log.d(TAG, "强制退出了");
    }


    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

运行结果及报错内容

当我在手机后台划掉APP时,主页面销毁日志要么没有,要么打印到“APP销毁”或“服务开始解绑”就没下文了。
而当我点击按钮进行服务解绑时,可以看到所有的日志:

2022-01-24 10:13:23.960 7790-7790/com.example.myapplication D/MyBindService: onUnbind: 
2022-01-24 10:13:23.960 7790-7790/com.example.myapplication D/MyBindService: onDestroy: 
2022-01-24 10:13:23.960 7790-7790/com.example.myapplication D/Tcpsocket: TCP线程开始释放资源了
2022-01-24 10:13:23.963 7790-9735/com.example.myapplication D/Tcpsocket: 接收数据失败了
2022-01-24 10:13:23.964 7790-7790/com.example.myapplication D/Tcpsocket: socket被关闭

这种情况下我怎么能判断当APP被关闭或者被回收后服务以及相关资源有没有正确释放呢?
希望大家不吝赐教。

直接划掉app,是不会有打印的。如果能改系统代码,可以在SystemUI里划动作kill APP是里面的逻辑。既然是划掉的,socket 资源系统会自动回收,不必考虑这种情况了,只考虑正常onDestroy情况即可

你可以把服务解绑和销毁放到super.onDestory()前面试试

1.你的主界面不是MainActivity吗?
2.你从MainActivity怎么返回主页面的?

img


这个时候 进程都没有了吧 去哪打印去
你可以打印本地日志看看 应该是有的

你直接划掉系统是会收到kill命令的,socket如果绑定在这个进程上得话也是直接被消掉的, onDestroy() 会在 onResume()之后

https://ask.csdn.net/questions/384736?spm=1005.2026.3001.5635&utm_medium=distribute.pc_relevant_ask_down.none-task-ask-2~default~OPENSEARCH~Rate-2.pc_feed_download_top3ask&depth_1-utm_source=distribute.pc_relevant_ask_down.none-task-ask-2~default~OPENSEARCH~Rate-2.pc_feed_download_top3ask

可以参考:https://blog.csdn.net/weixin_30338497/article/details/96898786