搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
Li_阴宅
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
一键安装tansformer_runtimeerror: failed to import transformers.pipeli
2
计算机基础应用期中试卷,计算机应用基础期中考试计算机试卷.doc
3
【思维与学习方法总结】面试_pdd 搜索2组
4
大模型/NLP/算法面试题总结1——大语言模型有哪些//泛化能力
5
速盾:高防CDN的特点与作用
6
数据结构——树的基本知识_数据结构树的知识点
7
医疗与大模型:重塑未来医疗生态的营销之道_医疗大模型市场需求
8
mysql5.7配置主从_mysql5.7主从配置
9
iOS APP 上架审核被拒Guideline 4.3 - Design,2.1,2.3.1,5.1.1解决方案_guideline 4.3(a) - design - spam
10
Android 10 音频焦点仲裁策略分析_android audio 仲裁
当前位置:
article
> 正文
Android 一个强大实用的版本升级demo(thread+service+Notification)_decsoft app builder 2020 (new thread)
作者:Li_阴宅 | 2024-07-20 14:51:25
赞
踩
decsoft app builder 2020 (new thread)
点击马上更新
点击后台更新
效果apk:
TestVersionUpdate.apk
(21.86 KB, 下载次数: 78)
源码:
versionUpdate.zip
(90.25 KB, 下载次数: 1312)
第一步:获取本机app版本:
public int getVerCode(Context _context,String _package) {
int verCode = -1;
try {
verCode = _context.getPackageManager().getPackageInfo(
_package, 0).versionCode;
} catch (NameNotFoundException e) {
}
return verCode;
}
复制代码
第二步:线程从服务端下载当前版本以及升级提示:
public JSONObject getJsonObject(String Url) {
HttpClient client = new DefaultHttpClient();
StringBuilder sb = new StringBuilder();
String js = null;JSONObject son=null;
HttpGet myget = new HttpGet(Url);
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 8000);
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
sb.append(s);
}
js = sb.toString();
son = new JSONObject(js);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("异常-》下载转化JSON");
return null;
}
return son;
}
复制代码
第三步:进行版本号对比,若有新版本,进行版本升级提示,builder使用自定义view。
LinearLayout ll = (LinearLayout) LayoutInflater.from(TestVersionUpdateActivity.this).inflate(
R.layout.layout_loadapk, null);
pb = (ProgressBar) ll.findViewById(R.id.down_pb);
tv = (TextView) ll.findViewById(R.id.tv);
Builder builder = new Builder(TestVersionUpdateActivity.this);
builder.setView(ll);builder.setTitle("版本更新进度提示");
builder.setNegativeButton("后台下载",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(TestVersionUpdateActivity.this, VersionService.class);
startService(intent);
dialog.dismiss();
}
});
builder.show();
new Thread() {
public void run() {
loadFile("http://1.nightman.sinaapp.com/test/good.zip");
}
}.start();
复制代码
第四步,进行更新
public void loadFile(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
float length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),
"NightMan.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
float count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
sendMsg(1,(int) (count*100/length));
}
}
sendMsg(2,0);
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e) {
sendMsg(-1,0);
}
}
private void sendMsg(int flag,int c) {
Message msg = new Message();
msg.what = flag;msg.arg1=c;
handler.sendMessage(msg);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 1:
pb.setProgress(msg.arg1);
loading_process = msg.arg1;
tv.setText("已为您加载了:" + loading_process + "%");
break;
case 2:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "NightMan.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(TestVersionUpdateActivity.this, error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
复制代码
后台下载service循环handler
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 1为出现,2为隐藏
if(TestVersionUpdateActivity.loading_process>99){
notificationMrg.cancel(0);
stopSelf();
return;
}
if(TestVersionUpdateActivity.loading_process>old_process){
displayNotificationMessage(TestVersionUpdateActivity.loading_process);
}
new Thread() {
public void run() {
isFirstStart=false;
Message msg = mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
}.start();
old_process =TestVersionUpdateActivity.loading_process;
}
};
复制代码
有需要改进的地方望大哥们多踢踢BUG
应部分朋友的需求,这边上一下
服务端
的代码
服务器端以PHP为例,输出一个JSON格式的字符串
<?php
echo '{ "version":2,
"content":[{"id":0,"text":"增加了摇一摇自动排列频道的功能"},
{"id":1,"text":"优化了拖拽缓冲的效果"},
{"id":2,"text":"改善了PATH菜单用户体验"},
{"id":3,"text":"添加了更多名人趣事"}
]}
';
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/Li_阴宅/article/detail/857295
推荐阅读
article
ansible
模块:
command
、
shell
、raw、copy、
hostname
、yum、serv...
ansible
模块:
command
、
shell
、raw、copy、
hostname
、yum、service、
user
、s...
赞
踩
article
CVE-2024-24549
Apache
Tomcat
-
Denial
of
Service
...
CVE-2024-24549
Apache
Tomcat
-
Denial
of
Service
CVE-2024-245...
赞
踩
article
【终极
解决方案
】为
应用程序
池“
XXX
”提供服务的进程在与
Windows
Process
Activ...
【终极
解决方案
】为
应用程序
池“
XXX
”提供服务的进程在与
Windows
Process
Activation
Serv...
赞
踩
article
PX4开发环境搭建2:首次执行
jmavsim
仿真不成功->
Exception
in
thread
"...
出错场景还原:输入:make px4_sitl_default
jmavsim
输出:...
Exception
in
...
赞
踩
article
Hystrix
-超时设置_
hystrix
isolation
thread
...
使用@
Hystrix
Command注解如果我们使用的是@
Hystrix
Command注解,那么可以在注解中直接指定超时时...
赞
踩
article
C++(标准库):46---并发之(细说启动线程:
async
、
future
、
shared
_futur...
待续_
c++
shared
state
c++
shared
state
上图是线程创建的...
赞
踩
article
探索现代
C++
微
服务
:
micro
-
service
...
探索现代
C++
微
服务
:
micro
-
service
在当今的云计算时代,微
服务
架构已经成为开发复杂应用的首选方案。而今天我们...
赞
踩
article
微
服务
架构之「
下一代
微
服务
Service
Mesh
」...
作者:奎哥来源:不止思考
Service
Mesh
被大家称为
下一代
的微
服务
,是微
服务
领域的一颗新星,被大家讨论的非常多。...
赞
踩
article
何为
面向
服务
(
Service
)?及其由来与规则_
c++
面向
服务
的
区别...
服务
是一组公开
的
功能集合。软件设计思想从函数发展到对象,从对象发展到组件,从组件发展到
服务
。
面向
函数20世纪80年代,...
赞
踩
article
云
原生
架构(微服务、容器云、
DevOps
、不可变
基础设施
、声明式API、
Serverless
、Ser...
微服务、容器云、
DevOps
、不可变
基础设施
、声明式API、
Serverless
、
Service
Mesh
_云
原生
架构云...
赞
踩
article
Prometheus
云原生 - 基于
file
_
sd
、
http
_
sd
实现
Service
Dis...
我们知道在
Prometheus
配置文件中可以通过 static_configs 来配置静态地址来获取数据,但是在云环...
赞
踩
article
Python 运行报错 ModuleNotFoundError: No module named '...
运行python程序,如果没有安装对应的模块,就会报如下的错误Traceback (
most
recent
call
l...
赞
踩
article
mac
本地搭建
ollama
_
mac
os
如何
找到
ollama
.
service
...
简介:
ollama
-webUI是一个功能强大的开源项目,简化了安装部署过程,并能直接管理各种大型语言模型(LLM)。本文...
赞
踩
article
Redis
笔记(01)— 安装、启动配置、开启远程连接、设置密码、远程连接_
redis
-serve...
Redis
简介1月份将 《
Redis
入门指南》过了一遍,现将
Redis
五大类型的常用命令做一总结,留着后续备用。R...
赞
踩
article
ExoPlayer
播放器
简单使用_
exoplayer
.
builder
...
使用
ExoPlayer
首先要创建一个playerr实例Simple
ExoPlayer
exoPlayer=new Sim...
赞
踩
article
AWS 提升
服务
配额
限制(
Service
Quota
Limit
)_
aws
提高
服务
器
配额
...
当我们想要创建一台EC2时,被告知cpu超出限制了,需要我们调整限制。_
aws
提高
服务
器
配额
aws
提高
服务
器
配额
...
赞
踩
article
C++
std
::
thread
管理
线程
生命周期
:
join
和
detach
...
方法用于同步主
线程
和子
线程
。也就是说,当主
线程
调用子
线程
的
join
方法时,主
线程
会被阻塞,直到子
线程
完成其执行。这意味着...
赞
踩
article
C++
11新特性:
std
:
:
thread
多线程
_
std
:
:
thread
th1
(
thread
...
在
C++
11以前,
C++
的
多线程
编程在不同到平台使用不同的API,比如linux平台使用p
thread
,windows平...
赞
踩
article
【
C++
包裹类
std
:
:
thread
】探索
C++
11
std
:
:
thread
:如何使用它来
创建
、...
探索
C++
11
std
:
:
thread
:如何使用它来
创建
、销毁和管理
线程
_
std
:
:
thread
销毁
std
:
:
thre...
赞
踩
article
C++
线程
库
std
:
:
thread
和
std
:
:
async
的用法详解_
std
:
:
thread
std
:
...
C++
作为一门强大的系统编程语言,提供了多种并发编程工具,使得开发者可以充分利用多核处理器的性能。
C++
11标准引入了新...
赞
踩
相关标签
ansible
linux
服务器
apache
tomcat
java
.Net
DMP文件调试
程序人生
IIS崩溃
SOA
云原生
架构
微服务
prometheus
服务发现
python
macos
llama
chatgpt
自然语言处理
Redis
aws
云计算