Android使用IntentService进行apk更新示例代码
作者:momentslz 时间:2022-01-19 12:12:07
通常在使用service更新应用时最常出现的问题就是Notification进度的更新问题、service在什么时间关闭以及需要我们自己在Service中创建新的线程处理耗时操作,当然这种也是可以实现的但是会显得略微繁琐
经过对比发现可以使用IntentService已经实现了对耗时操作的包装出来,我们只需要实现IntentService中的onHandleIntent方法就可以在其中进行耗时操作的处理,在处理下载问题时发现在使用intentservice时暂时没有发现可以优雅的进行进度回调的实现方法,所以我这边使用了本地广播的形式来进行进度刷新。
添加了当前状态判断,当应用处于前台状态时直接进行安装,当应用处于后台时弹出notification弹窗点击后安装,示例如下图:
先创建广播
public static class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case ACTION_TYPE_PREPARE:
if (downloadCallback != null) {
downloadCallback.onPrepare();
}
break;
case ACTION_TYPE_PROGRESS:
int progress = intent.getIntExtra("progress", 0);
// Log.d("progress", "|- " + progress + " -|");
if (downloadCallback != null) {
downloadCallback.onProgress(progress);
}
break;
case ACTION_TYPE_COMPLETE:
String file_path = intent.getStringExtra("file_path");
if (!TextUtils.isEmpty(file_path)) {
File file = new File(file_path);
if (file.exists()) {
if (downloadCallback != null) {
downloadCallback.onComplete(file);
}
}
}
break;
case ACTION_TYPE_FAIL:
String error = intent.getStringExtra("error");
if (downloadCallback != null) {
downloadCallback.onFail(error + "");
}
break;
}
}
然后在IntentService中初始化本地广播并发送信息
@Override
public void onCreate() {
super.onCreate();
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
}
// 在下载进度刷新的地方进行回调
private void progress(int progress) {
Intent intent = new Intent(FileDownloaderManager.ACTION_TYPE_PROGRESS);
intent.putExtra("progress", progress);
mLocalBroadcastManager.sendBroadcast(intent);
}
private void downApk(String url) {
.....
.....
progress(progress);
.....
.....
}
在activity中使用
mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_TYPE_PREPARE);
intentFilter.addAction(ACTION_TYPE_PROGRESS);
intentFilter.addAction(ACTION_TYPE_COMPLETE);
intentFilter.addAction(ACTION_TYPE_FAIL);
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
// ondestory时调用
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
以上源码已进行封装,方便使用具体操作步骤如下:
|- 初始化及注册回调
//初始化文件下载管理类
FileDownloaderManager.init(context)
// 注册下载进度监听,并开启广播接收
FileDownloaderManager.registerDownload(object : FileDownloaderManager.DownloadCallback {
override fun onComplete(file: File) = mainView.downloadSucc(file)
override fun onFail(msg: String?) = Unit
override fun onProgress(progress: Int) = mainView.onProgress(progress)
override fun onPrepare() = Unit
})
//开始下载
FileDownloaderManager.download(url)
|- 在下载完成后进行资源重置
FileDownloaderManager.unbinder()
源码地址:源码地址
文档地址:文档地址
来源:https://www.jianshu.com/p/833abc5ee9d0
标签:IntentService,apk,更新
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis-plus update更新操作的三种方式(小结)
2023-10-08 14:05:08
![](https://img.aspxhome.com/file/2023/9/62639_0s.png)
Java使用桥接模式实现开关和电灯照明功能详解
2022-05-18 06:20:35
![](https://img.aspxhome.com/file/2023/8/84288_0s.png)
Java实现读取163邮箱,qq邮箱的邮件内容
2021-09-27 14:26:31
![](https://img.aspxhome.com/file/2023/9/63159_0s.png)
spring data JPA 中的多属性排序方式
2023-01-14 19:26:41
Spring Batch批处理框架使用解析
2021-12-24 03:41:19
springboot如何读取配置文件到静态工具类
2023-11-28 04:44:54
![](https://img.aspxhome.com/file/2023/6/60036_0s.png)
C# SynchronizationContext以及Send和Post使用解读
2023-10-16 04:27:28
springboot vue组件开发实现接口断言功能
2023-11-12 10:26:53
![](https://img.aspxhome.com/file/2023/9/61869_0s.gif)
C#-WinForm跨线程修改UI界面的示例
2022-01-14 13:25:44
![](https://img.aspxhome.com/file/2023/0/76420_0s.png)
java实现小猫钓鱼游戏
2021-10-10 19:59:48
WebView的几个常见功能使用方法
2023-01-25 13:54:48
Spring定时任务中@PostConstruct被多次执行异常的分析与解决
2022-08-20 07:28:22
ArrayList和LinkedList的区别、扩容机制以及底层的实现方式
2023-11-27 01:26:57
![](https://img.aspxhome.com/file/2023/9/60279_0s.png)
深入理解Android 5.0中的Toolbar
2022-08-28 02:26:39
![](https://img.aspxhome.com/file/2023/1/98651_0s.png)
Java数据结构与算法入门实例详解
2023-11-28 21:44:06
![](https://img.aspxhome.com/file/2023/0/60280_0s.png)
JAVA中对List进行查询
2023-12-17 20:41:20
Java 深入探讨设计模式之原型模式篇
2023-11-16 17:37:59
![](https://img.aspxhome.com/file/2023/3/64553_0s.png)
Java线程安全解决方案(synchronized,ReentrantLock,Atomic)
2022-06-13 12:51:09
Android WebView使用的技巧与一些坑
2022-10-18 12:57:23
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2022-02-28 02:43:26
![](https://img.aspxhome.com/file/2023/2/85542_0s.jpg)