Android应用开发——service连接泄露异常:android.app.ServiceConnectionLeaked: that was originally bound here

记一次解决MQTT软件包内存泄露的心路历程 1、技术背景 物联网产品使用的mqtt连接功能采用的kawii-mqtt软件包,具体的软件包地址在:[kawii mqtt软件包地址](https://github.com/longtengmcu/kawaii-mqtt),当出基于此软件包开发时,解决了此软件包的许多问题(可查看git 提交记录),包括内存泄漏问题,现在已经成功应用在产品上,并且稳定运行。目前的产品应用是mqtt做的长连接,即创建连接后,应用程序不会主动断开连接,可以稳定运行。 最近开发产品由于要做低功耗... 阅读详情

在做service开发过程中,大部分可能会遇到以下异常,该异常仅通过log输出,并不会导致app crash。

E/ActivityThread: Activity com.example.image.all_samples.Main2Activity has leaked ServiceConnection com.example.image.all_samples.Main2Activity$5@d124870 that was originally bound here
                   android.app.ServiceConnectionLeaked: Activity com.example.image.all_samples.Main2Activity has leaked ServiceConnection com.example.image.all_samples.Main2Activity$5@d124870 that was originally bound here
                       at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1369)
                       at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1264)
                       at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1454)
                       at android.app.ContextImpl.bindService(ContextImpl.java:1426)
                       at android.content.ContextWrapper.bindService(ContextWrapper.java:636)
                       at com.example.image.all_samples.Main2Activity$3.onClick(Main2Activity.java:53)
                       at android.view.View.performClick(View.java:5724)
                       at android.view.View$PerformClick.run(View.java:22572)
                       at android.os.Handler.handleCallback(Handler.java:751)
                       at android.os.Handler.dispatchMessage(Handler.java:95)
                       at android.os.Looper.loop(Looper.java:154)
                       at android.app.ActivityThread.main(ActivityThread.java:6259)
                       at java.lang.reflect.Method.invoke(Native Method)
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)

异常原因:
比较简单,一般为在activity退出时未调用unbindService导致,只需要添加unbindService接口即可。

进一步思考:android系统是如何检测到给泄漏的呢。
很容想到bindService时,是通过当前activity的context进行bind的,所以service的生命周期应该是同改activity关联,进一步验证,bindService时通过application的context进行bind,退出当前activity时,不会报上面的异常错误,基本可以证明service的生命周期同bindService时依赖的context相关,所以如果需要一个不依赖于具体activity生命周期的service,需要用application的context进行bind。

Android - 对接MQTT 首先你得知道这是什么吧,稀里糊涂抄一大堆完事就忘了可不行。MQTT(Message Queuing Telemetry Transport,消息队列遥测传输),MQTT是一个客户端服务端架构的发布/订阅模式的消息传输协议。MQTT有MQTT基本就是这些,本文默认你已经有一个MQTT的服务地址了,接下来就负责Andriod连接。我的使用场景是打开在线调试,一对一看到对方发来的数据。 阅读详情

相关推荐

ROS2+MoveIt2+Gazebo仿真实战:从零搭建六轴机械臂控制闭环

本文详细介绍了如何从零搭建一个完整的ROS2+MoveIt2+Gazebo六轴机械臂仿真控制闭环。通过手写URDF模型、配置MoveIt2运动规划、集成Gazebo物理仿真与ROS2 Control控制器,最终实现RViz中规划轨迹、Gazebo中同步执行的完整数字孪生验证平台,为机器人算法开发和硬件验证提供低成本、高效的仿真解决方案。

weixin_29093017的博客 485

Service启动泄露异常:android.app.ServiceConnectionLeaked

项目场景: 在子线程中启动服务 在Activity中启动服务 问题描述: 软件在运行过程中,相关服务抛出异常:android.app.ServiceConnectionLeaked 详细日志: Service com.xxx.xxx.alexaservice.MetroAlexaAudioProviderService has leaked ServiceConnection com.xxx.xxx.connection.MetroConnection@32dc748 that was origina

一个IT人的技术成长历程。 5402

AOSP Android14 Launcher3——动画核心类QuickstepTransitionManager详解

是一个复杂但核心的动画管理器。它利用 Android 的远程动画机制,拦截系统默认的窗口切换,并替换为高度定制、与 Launcher UI 深度集成的平滑过渡动画。当用户从一个应用程序通过手势或按键返回到 Launcher (桌面) 时,它负责创建和运行动画,使得应用程序窗口能够平滑地过渡(通常是缩小)到 Launcher 上的对应目标(图标、小部件或屏幕中心),同时 Launcher 界面元素(工作区、背景等)恢复显示。计算动画所需的各种参数,如窗口的起始/结束位置、缩放比例、透明度、圆角、裁剪区域等。

Micheal_Yang's Blog 2669

Error:android.app.ServiceConnectionLeaked:就是leaked ServiceConnection的错误

在做activity和service绑定时候,出现了这个错误。 android.app.ServiceConnectionLeaked: Activity com.dxn.xtgascehui.activity.MappingActivity has leaked XServiceConnection  com.dxn.xtgascehui.activity.MappingActivity$2

Mar_cjc的博客 7335

PyTorch深度学习实战(39)——少样本学习(Few-shot Learning)

小样本学习 (Few-shot Learning) 旨在解决在训练集中只有很少样本的情况下进行分类和推理的问题。在小样本学习中,我们希望通过利用已有的少量样本和先验知识来进行泛化,以便在面对新的、未见过的类别时能够做出准确的预测。这就要求模型能够从有限的训练样本中提取出有用的信息,并能够将这些信息应用到新类别的样本中去。在本节中,将介绍孪生、原型和关系网络的原理,并使用 PyTorch 实现孪生网络。

盼小辉丶的博客 1万+

30天入门 SAP BTP 开发系列之三:了解 SAP BTP Global Account 的概念

90天之后 Trial 账号会被删除,该账号在 SAP BTP 上创建的内容会被清空,不过我们可以再创建一个新的 Tria 账号。本文简单介绍了 SAP BTP 里 Global Account 的概念,下一篇文章我会介绍 SAP BTP 控制台里和 Global Account 相关的一些操作。,创建 SAP BTP 的 Trial,在可选 Region 列表里,在两个可选项中二选一,选择了。或订阅合同时,SAP BTP 会在后台为其生成一个对应的。在企业拥抱云原生架构的过程中,SAP BTP 的。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 999

Android开发之MQTT的使用

首先MQTT是啥子东西?新的项目要用到这个,不过我之前从来没有听说过啊。 没办法,许多事情是不能等你准备就绪才可以去做的,生活就是这样。 好了,接下来通过上网查阅资料,关于MQTT的初步认识已经形成了,下面是摘录的一些介绍: 原文地址 Android端实现消息推送的几种方式 轮询:客户端定时向服务器请求数据。伪推送。缺点:费电,费流量。 拦截短信消息。服务器需要向客户端发通...

asjqkkkk的博客 7万+

【ArcGIS Pro微课1000例】0047:深度学习--棕榈树提取全流程

对汤加科洛瓦伊种植园每棵棕榈树的健康状况进行清查和评估,这需要花费大量的时间和劳动力。为简化此过程,将在 ArcGIS Pro 中使用深度学习模型来识别树木,然后根据植被绿度的测量值计算其健康状况。第一步是找到显示汤加科洛瓦伊的影像,该影像具有足够精细的空间和光谱分辨率来识别树木。获得此影像后,将创建训练样本并将其转换为可供深度学习模型使用的格式。为了让模型识别出它的查找任务,需要定义棕榈树的影像,以便识别相似的像素和树木大小。

智慧空间实验室 2207

Android bindService报错:has leaked ServiceConnection ***that was originally bound here

在android的activity中bindService,出现了“has leaked ServiceConnection ***that was originally bound here”的bug。 一开始怎么也不明白为什么会有这个bug,抓狂了。后来静下来心来,仔细的分析一个这个bug ***has leaked ServiceConnection ***.***.activities.***Activity$3@bfb2c04 that was originally bound here

weixin_42602900的博客 9051

R语言coxph函数对加权数据集进行竞争风险模型拟合、regplot包的regplot函数可视化竞争风险回归模型的列线图nomogram、计算特定病例的事件概率(控制了竞争风险的累计事件概率)

R语言coxph函数对加权数据集进行竞争风险模型拟合、regplot包的regplot函数可视化竞争风险回归模型的列线图nomogram、计算特定病例的事件概率(控制了竞争风险的累计事件概率)

statistics+insight+vista+power 359

蓝鸟四轴飞控系统C程序V3.0(基础版本)深度解读

2. 解析遥控通道数据(油门 / 横滚 / 俯仰 / 偏航),转换为标准化值(-1000~+1000);3. 封装PID初始化(PID_Init,配置KP/KI/KD、输出限幅、积分限幅);,包含姿态角(Attitude_Data.Roll/Pitch/Yaw)、角速度(Gyro_Data.X/Y/Z)、遥控值(RC_Data[0~5])、电池电压(Bat_Volt)、电机PWM值(Motor_PWM[0~3])、系统状态(Sys_State),校验和通过Check_Sum()函数计算;

搏博的专栏 344

Android开发Service之BindService

前言 一直使用 Qt 来进行桌面端和移动端开发,对 Android 开发并不熟悉,但是项目中往往又经常会用到 Android 原生开发的东西。本次收集了一些关于BindService相关的知识供自己学习和参考。 正文 bindService介绍 一、bindService简介 bindService是绑定Service服务,执行service服务中的逻辑流程。 service通过C...

luoyayun361的专栏 1496

2026.2.11:ollama api推理GLM-OCR模型识别图片

本文介绍了使用Ollama API调用GLM-OCR模型进行图片文字识别的过程。首先通过ollama pull glm-ocr:latest命令拉取模型,然后使用Python脚本调用API接口。代码示例展示了如何发送包含图片路径的请求,并处理返回的识别结果,支持文本、公式和表格三种识别模式。运行结果展示了模型对输入图片的识别效果,为开发者提供了便捷的OCR解决方案。

拥抱具身智能,昇腾Ascend C 367

(原)android系统下绑定Server的时候报MainActivity has leaked ServiceConnection的错误

今天在android系统下根据官方的demo代码,我们需要启动一个服务,并绑定,但在程序启动以后,老是报错: ActivityMainActivityhasleakedServiceConnection CameraUtil$ServiceBinder@38fe0435thatwasoriginallyboundhere android.app.Servi...

dianchixian5277的博客 1353

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

has leaked ServiceConnection

当我们采用bindService(service, conn, flags)而不是 startService(service)启动服务Service的时候,如果我们在关闭Activity的时候没有释放链接,此时就会出现 **** has leaked ServiceConnection等错误日志,意思就是服务连接泄露(因为在关闭Acitivity的时候没有释放链接),这个错误就好像我们启动了一个对

戏耍明天的专栏 1812

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

解决has leaked ServiceConnection com.baidu.location.c@427b6780

解决“has leaked ServiceConnection com.baidu.location.c@427b6780 that was originally bound here”的错误,需要修改AndroidManifest.xml文件。 配置Map定位权限和远程链接机制。不然,就会报下面的错误: [plain] view plaincopyprint?

u011057161的专栏 4318

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

百度地图定位功能的错误has leaked ServiceConnection 解决

首先感谢博主总结: http://blog.csdn.net/u010940300/article/details/43488651 再次在博主的基础上补充说明: 1、androidmanifest文件中application节点下添加 备注: 新版的sdk 用这个                   android:name="com.baidu.location.f"   

zww986736788的博客 1598

Android学习记录<三>

两个错误处理: 1.android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked Servi... 该异常是因为android的模拟器上自带的邮件应用的问题,所以需要对模拟器进行一些设置,禁用此类无关的应用,具体操作为:“设置”---->"所有应用"---->"Excha

yun2205446823的专栏 469

出现leaked ServiceConnection 的解决办法

出现leaked ServiceConnection 的解决办法Activity com.skyace.service.MainActivity has leaked ServiceConnection com.phicomm.hu.MainActivity that was originally bound here android.app.ServiceConnectionLeaked: Act

Android developer. 5万+

Android学习笔记---记一个ServiceConnectionLeaked连接服务器异常

前言 某天更新版本的时候发现一个问题,就是连接MQTT服务器的时候会出现 android.app.ServiceConnectionLeaked: Service com.allthings.iwarehousesystem.service.MQTTService has leaked ServiceConnection org.eclipse.paho.android.s

CrackgmKey的博客 3669

Android MQTT 反复重连故障

即 检查mqttAndroidClient = new MqttAndroidClient(this, serverURI, CLIENTID);中的CLIENTID是否唯一,不同设备需要用不同的id。检查WiFi是否连接或者稳不稳定。例如可使用Mqttx测试工具。

shi450561200的博客 1482

Android开发之服务连接泄露异常

写了个Android service,用来完成一些耗时的网络IO和远程设备连接。在Activity中,先用startService( )启动服务,然后调用bindService( )将Activity邦定到该服务上,最后执行程序。看起来程序运行没有问题,不过Log显示出现一个异常,如下: E/ActivityThread(12467): Activity com.raycommtech.rayf

harry_helei的专栏 1万+

Android内存泄漏

内存泄漏:生命周期较长的对象引用了生命周期较短的对象,导致生命周期较短的对象无法被GC及时回收,从而导致被占用的内存无法释放。

mxiaomi的专栏 1544

android serviceconnection unbind流程,出现 leaked ServiceConnection 解决办法

package com.jiuqi.testservice01;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;i...

weixin_39542111的博客 416
上一篇: Android应用开发——文件目录
下一篇: Iot相关杂烩
voidreturn
博客等级 码龄10年 53粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值