在moudle级别下的build.gradle文件中添加依赖
// 添加maven仓库
repositories {
maven {
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
}
}
// 添加依赖库
dependencies {
implementation 'com.gitee.chinasoft_ohos:ohos-weak-handler:1.0.1'
}
如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下
Problem
Original implementation of Handler always keeps hard reference to handler in queue of execution.
Any object in Message or Runnable posted to ohos.eventhandler.EventHandler
will be hard referenced for some time.
If you create anonymous Runnable and call to postTask
with large timeout, that Runnable will be held
in memory until timeout passes. Even if your Runnable seems small, it indirectly references owner class,
which is usually something as big as Activity or Fragment.
You can read more on our blog post.
Solution
WeakHandler
is trickier than ohos.eventhandler.EventHandler
, it will keep WeakReferences
to runnables and messages,
and GC could collect them once WeakHandler
instance is not referenced any more.
Usage
Use WeakHandler as you normally would use Handler
import com.badoo.mobileDemo.util.WeakHandler;
public class ExampleAbility extends Ability {
private WeakHandler handler; // We still need at least one hard reference to WeakHandler
@Override
public void onStart(Intent intent) {
handler = new WeakHandler();
...
}
private void onClick(Component view) {
handler.postTask(new Runnable() {
view.setVisibility(Component.VISIBLE);
}, 5000);
}
}
CodeCheck代码测试无异常
CloudTest代码测试无异常
安全病毒安全检测通过
当前版本demo功能与原组件基本无差异
Weak Handler is brought to you by Badoo Trading Limited and it is released under the MIT License.
Created by Dmytro Voronkevych
##Blog Read more on our tech blog or explore our other open source projects
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型