leowong87 2022-01-24 10:20 采纳率: 60%
浏览 164
已结题

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被关闭或者被回收后服务以及相关资源有没有正确释放呢?
希望大家不吝赐教。

  • 写回答

7条回答 默认 最新

  • Ztian77 2022-01-24 15:17
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 2月1日
  • 已采纳回答 1月24日
  • 创建了问题 1月24日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan