Android如何更新app的版本(中级)
版本更新
看看我们要用到哪些技术1 自定义通知栏
2 HTTP 下载
3 AsyncTask
4 刷新通知栏中的进度条
5 执行 apk安装的隐士意图
6 Toast
7签名(安装时系统会自动检测签名是否一致)
8获得服务端和客户端的版本号
上代码
(1)点击事件判断是否有新版本更新 (2)自定义一个通知 同时刷新下载进度
(3) 异步下载新版本app(4) 隐士意图来安装
首先别忘了写权限!!!!!超爱忘得东西 恨死它了
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission>
private Runnable mrun;
private NotificationManager mNotificationManager;
private Notification notification;
private RemoteViews remoteviews;
private int count;
private int loadversion;
private int version;
//升级按钮点击事件的方法 通过服务器来解析JSON 得到版本号 判断是否来通知下载
private void upgrade() {
PackageManager nPackageManager=getPackageManager();//得到包管理器
try {
PackageInfo nPackageInfo=nPackageManager
.getPackageInfo(getPackageName(),PackageManager.GET_CONFIGURATIONS );
loadversion=nPackageInfo.versionCode;//得到现在app的版本号
} catch (NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//服务器端通过GET 得到JSON
String json=JSandBitmap.httpGetDemo("http://192.168.14.234/version.json");
try {
JSONArray jsonArray=new JSONArray(json);
JSONObject jsonObject=jsonArray.getJSONObject(0);
version = jsonObject.getInt("version");//得到服务端的app版本号
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if((loadversion<version)){//如果有新版本发送通知开始下载
sendNotification();
}else{//如果没有 弹出对话框告知用户
new AlertDialog.Builder(this)
.setTitle("update cancel")
.setMessage("Sorry Not new Version ")
.setNegativeButton("cencel", null).show();
}
}
//此方法来发送下载通知 采用的是自定义通知栏 并且更加下载的进度来刷新进度条
//自定义通知的方法 在上上篇的博文中 这里不做太多的解释
private void sendNotification() {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(
R.drawable.player_play_light, "Midiplay",
System.currentTimeMillis());
remoteviews= new RemoteViews("com.tarena.gsd110623.midiplayonline", R.layout.mynotiifcation);
remoteviews.setImageViewResource(R.id.p_w_picpathView1, R.drawable.a00);
notification.contentView=remoteviews;
Intent intent=new Intent(this,install.class);//PendingIntent 调用的系统的安装隐士意图 后面红色的代码
PendingIntent pendingintent=PendingIntent.getActivity(this, 0, intent, 0);
notification.contentIntent=pendingintent;
mrun=new Runnable() {//这个Runnable 用来根据下载进度来刷新进度条
@Override
public void run() {
if(count<98){//紫色的count 是异步下载计算出来设置进度的值
remoteviews.setProgressBar(R.id.progressBar1, 100, count, false);
remoteviews.setTextViewText(R.id.textView1, count+"%");
mNotificationManager.notify(8888, notification);
handler.postDelayed(mrun, 300);
}else{//这里计算出来的count 不是那么准确 所以下载完成后 给一个固定值做为下载完成
remoteviews.setProgressBar(R.id.progressBar1, 100, 100, false);
remoteviews.setTextViewText(R.id.textView1, 100+"%");
mNotificationManager.notify(8888, notification);
Toast.makeText(Welcome.this, "download over", Toast.LENGTH_SHORT);//提示用户下载成功
}
}
};
handler.postDelayed(mrun, 300);
Update_AsyncTask mUpdate_AsyncTask=new Update_AsyncTask();
try {//启动下载
mUpdate_AsyncTask.execute(new URL("http://192.168.14.234/android_project_midiplayonline.apk"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//这个内部类用来异步下载新版本app 通过服务端下载这里不多说了
class Update_AsyncTask extends AsyncTask<URL, Integer, Object>{
@Override
protected Object doInBackground(URL... params) {
// TODO Auto-generated method stub
try {
URLConnection con = params[0].openConnection();
if (HttpURLConnection.HTTP_OK != ((HttpURLConnection)con).getResponseCode())
{
Log.i("Main", "connection failed");
return null;
}
InputStream is = con.getInputStream();
int contentlength=con.getContentLength();//得到下载的总长度
System.out.println(contentlength);
File file=new File(Constant.APK_FILE_PATH);
if(!file.exists()){
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream out=new FileOutputStream(file);
int current = 0;
int x=0;
byte[]arr=new byte[1024];
while ( (current = is.read(arr)) != -1 ){
out.write(arr, 0, current);
x=x+current;
count=(int)(100*x/contentlength);//计算下载的百分百
}
is.close();
} catch (Exception e) {
}return null;
}
}
/*
* 此类为系统安装的隐士意图
*/
public class install extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent notify_Intent = new Intent(Intent.ACTION_VIEW);
notify_Intent.setDataAndType(Uri.fromFile(new File(Constant.APK_FILE_PATH)), "application/vnd.android.package-archive");
startActivity(notify_Intent);
//取消上一个通知
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(8888);
overridePendingTransition(0, 0);
finish();
}
}
效果如下

 

转载于:https://blog.51cto.com/skyoceanone/677114

Android如何更新app的版本(中级)相关推荐

  1. android ota更新app,企业 OTA 更新  |  Android 开源项目  |  Android Open Source Project

    Android 兼容性定义文档 (CDD) 可更新软件要求设备实现 SystemUpdatePolicy 类.SystemUpdatePolicy 可让设备所有者 (DO) 应用(如果存在)控制系统更 ...

  2. Android Studio 更新到指定版本

    一. 检测并更新最新版本 查看Android Studio 的版本: 检测及版本更新: ①Help --> Check for Updates... ② File --> Settings ...

  3. Android应用更新-自动检测版本及自动升级

    步骤: 1.检测当前版本的信息AndroidManifest.xml–>manifest–>[Android] 2.从服务器获取版本号(版本号存在于xml文件中)并与当前检测到的版本进行匹 ...

  4. Android 天气APP(二十六)增加自动更新(检查版本、通知栏下载、自动安装)

    上一篇:Android 天气APP(二十五)地图天气(下)嵌套滑动布局渲染天气数据 效果图 开发流程 1.开发前言 2.上传应用到分发平台 3.版本数据请求与存储 4.检查版本更新.自定义更新提示弹窗 ...

  5. Android 增量更新实例(Smart App Updates)

    http://892848153.iteye.com/blog/2022851 转自:http://my.oschina.net/liucundong/blog/160436 目录[-] 官方说明 实 ...

  6. android 自动更新apk版本

    原文地址:android 自动更新apk版本 截图如下: 代码实现如下: package com.update.apk;import java.io.BufferedReader; import ja ...

  7. Android如何实现APP自动更新

    先来看看要实现的效果图: 对于安卓用户来说,手机应用市场说满天飞可是一点都不夸张,比如小米,魅族,百度,360,机锋,应用宝等等,当我们想上线一款新版本APP时,先不说渠道打包的麻烦,单纯指上传APP ...

  8. android用什么更新应用程序,如何在Android上更新应用程序 教你如何更新安卓手机APP...

    您从Play商店下载的大多数Android应用程序都会出于各种原因而定期提供更新:添加功能,错误修复,提高安全性...了解如何在Android智能手机上管理应用程序以及如何使它们保持最新. 如果您想要 ...

  9. Android安装更新 apk,适用于android6.0及以上安卓版本。

    Android应用中apk下载更新,适用于android 9及以下安卓版本. 直接上代码: 一.在主配置文件中写权限. <uses-permission android:name="a ...

最新文章

  1. 关于远程办公,微软MVP 15年研发团队的经验分享
  2. compileReleaseJavaWithJavac
  3. 前端学习(2607):vue指令
  4. python 覆盖list_【Python妙招】gt;gt;gt;看腻了能不能换成别的啊……当然可以啦:)...
  5. linux oa软件安装步骤,Ecology_OA_for_Linux安装手册
  6. 训练caffe:registry.count(type) == 0 (1 vs. 0) Solver type Nesterov already registered
  7. oracle右模糊查询不使用索引,oracle like模糊查询不能走索引?
  8. python 笔记 之 装饰器
  9. Linux 内存管理之 SLUB分配器 (4):slub page大小计算方法
  10. 函数分离常数法 oracle,2009届高三数学第一轮复习课件:函数(最新)幻灯片
  11. DOS控制台启动方式+DOS控制台常用命令
  12. 计算机round是什么函数,round()函数,excel round什么意思
  13. 基于PyQt5与opencv制作的证件照尺寸变换应用程序
  14. 孙子兵法始计篇读后感&心得(下)
  15. CUBA Platform
  16. 如何提高关键词的质量度?
  17. CCNA-静态路由实验
  18. 超强高温天气来袭,“幕后推手”是谁
  19. java IO学习心得
  20. WPS当中封面图如何快速对齐下划线

热门文章

  1. Java多线程之Callable接口的实现
  2. 用Vue撸一个『A-Z字母滑动检索菜单』
  3. 比特币要升级成为“比特币现金”
  4. AngularJS控制div隐藏或显示-ng-show
  5. apache与tomcat连接
  6. SQL Server 2005实现负载均衡的详细介绍
  7. Delphi常用时间函数列表
  8. AIO-3128C四核高性能主板
  9. 怎样实现企业管理系统的操作日志功能
  10. Visual Studio 20年