安卓实现在线更新
来源:互联网 发布:上海知柚公司官网图片 编辑:程序博客网 时间:2024/05/01 16:05
版权声明:本文为博主原创文章,未经博主允许不得转载。
////////////////////2016/04/25//////////////////////
///////////////////by XBW///////////////////////////
//////////////////环境 eclipse api22///////////
做点贡献,发几个小demo,每次启动某个app都会遇到app升级,这是怎么实现的呢,先上图吧,
有图有真相,
这截图真大,没睡了,
结构。
Config.java
- package com.example.update;
- public class Config {
- public static String mysql_url_update="http://127.0.0.1/androidapp/appupdate/update.txt";//服务器端更新url;
- }
FileUtil.java
- package com.example.update;
- import java.io.File;
- import java.io.IOException;
- import android.os.Environment;
- public class FileUtil {
- public static File updateDir = null;
- public static File updateFile = null;
- public static final String Application = "konkaUpdateApplication";
- public static boolean isCreateFileSucess;
- public static void createFile(String app_name) {
- if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
- isCreateFileSucess = true;
- updateDir = new File(Environment.getExternalStorageDirectory()+ "/" +Application +"/");
- updateFile = new File(updateDir + "/" + app_name + ".apk");
- if (!updateDir.exists()) {
- updateDir.mkdirs();
- }
- if (!updateFile.exists()) {
- try {
- updateFile.createNewFile();
- } catch (IOException e) {
- isCreateFileSucess = false;
- e.printStackTrace();
- }
- }
- }else{
- isCreateFileSucess = false;
- }
- }
- }
GetVersion.java
- package com.example.update;
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- public class GetVersion {
- // 获取当前版本的版本号
- public String getVersion(Context context) {
- try {
- PackageManager packageManager = context.getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(
- context.getPackageName(), 0);
- return packageInfo.versionName;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- return "版本号未知";
- }
- }
- }
IsNeedUpdate.java
- package com.example.update;
- import android.content.Context;
- public class IsNeedUpdate {
- private UpdateInfo info;
- public IsNeedUpdate(Context context){
- UpdateInfoService updateInfoService = new UpdateInfoService(
- context);
- try {
- info = updateInfoService.getUpDateInfo();
- } catch (Exception e) {
- // TODO 自动生成�? catch �?
- e.printStackTrace();
- }
- }
- public boolean isNeedUpdate(Context context) {
- GetVersion version=new GetVersion();
- //版本更新检测使用浮点型存在小版本问题,浮点型不能识别v 1.2.1这种小版本
- //double webVersion=Double.parseDouble(info.getVersion());
- // double localVersion=Double.parseDouble(version.getVersion(context));
- //采用比较字典序大小检测版本更新
- if (info.getVersion().compareTo(version.getVersion(context))>0) {
- return true;
- } else {
- return false;
- }
- }
- public String getDescribe(){
- return info.getDescription();
- }
- public String getUrl(){
- return info.getUrl();
- }
- }
ShowUpdateDialog.java
- package com.example.update;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Environment;
- import android.text.method.ScrollingMovementMethod;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageButton;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ShowUpdateDialog{
- private UpdateInfo info;
- //更新窗口
- public Dialog showUpdateDialog(final Context context,String msg,final String url) {
- LayoutInflater layoutInflater =LayoutInflater.from(context);
- RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );
- final Dialog dialog = new AlertDialog.Builder(context).create();
- dialog.show();
- dialog.getWindow().setContentView(layout);
- TextView tex=(TextView)layout.findViewById(R.id.dialog_text);
- TextView tex1=(TextView)layout.findViewById(R.id.textView_title);
- tex.setMovementMethod(ScrollingMovementMethod.getInstance());
- tex.setText(msg);
- tex1.setText("更新提示");
- //确定按钮
- Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
- btnOK.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
- //services.service();
- Intent intent = new Intent(context,UpdateServices.class);
- intent.putExtra("Key_App_Name",context.getString(R.string.app_name));
- intent.putExtra("Key_Down_Url",url);
- context.startService(intent);
- } else {
- Toast.makeText(context, "SD卡不可用,请插入SD卡",
- Toast.LENGTH_SHORT).show();
- }
- dialog.dismiss();
- }
- });
- //关闭按钮
- ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
- btnClose.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- }
- });
- return dialog;
- }
- //不更新窗口
- public Dialog showDialog(final Context context) {
- LayoutInflater layoutInflater =LayoutInflater.from(context);
- RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );
- final Dialog dialog = new AlertDialog.Builder(context).create();
- dialog.show();
- dialog.getWindow().setContentView(layout);
- TextView tex=(TextView)layout.findViewById(R.id.dialog_text);
- TextView tex1=(TextView)layout.findViewById(R.id.textView_title);
- GetVersion version=new GetVersion();
- tex.setText("您使用的是最新版:"+version.getVersion(context)+"版本");
- tex1.setText("更新提示");
- Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
- btnOK.setVisibility(View.INVISIBLE);
- //关闭按钮
- ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
- btnClose.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- }
- });
- return dialog;
- }
- }
UpdateInfo.java
- package com.example.update;
- public class UpdateInfo
- {
- private String version;
- private String description;
- private String url;
- public String getVersion()
- {
- return version;
- }
- public void setVersion(String version)
- {
- this.version = version;
- }
- public String getDescription()
- {
- return description;
- }
- public void setDescription(String description)
- {
- this.description = description;
- }
- public String getUrl()
- {
- return url;
- }
- public void setUrl(String url)
- {
- this.url = url;
- }
- }
UpdateInfoService.java
- package com.example.update;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.content.Context;
- public class UpdateInfoService {
- public UpdateInfoService(Context context) {
- }
- public UpdateInfo getUpDateInfo() throws Exception {
- String path = Config.mysql_url_update;
- StringBuffer sb = new StringBuffer();
- String line = null;
- BufferedReader reader = null;
- UpdateInfo updateInfo = new UpdateInfo();
- try {
- // 创建�?个url对象
- URL url = new URL(path);
- // 通過url对象,创建一个HttpURLConnection对象(连接)
- HttpURLConnection urlConnection = (HttpURLConnection) url
- .openConnection();
- // 通过HttpURLConnection对象,得到InputStream
- reader = new BufferedReader(new InputStreamReader(
- urlConnection.getInputStream()));
- // 使用io流读取文�?
- while ((line = reader.readLine()) != null) {
- sb.append(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- String info = sb.toString();
- updateInfo.setVersion(info.split("&")[1]);
- updateInfo.setDescription(info.split("&")[2]);
- updateInfo.setUrl(info.split("&")[3]);
- return updateInfo;
- }
- }
才发现csdn博客还有长度限制,发了好几次都被截断了,这样吧,
直接传送demo,或者去我小站看一下吧
demo传送门
小站
小站demo下载不需要积分哦
0 0
- 安卓实现在线更新
- 安卓实现在线更新
- 产品更新发布《一个星期自学安卓》在线课堂
- C#实现在线更新
- 在线更新的实现1
- 在线更新的实现2
- AppStore在线更新提示实现
- 实现Android的在线更新
- github代码实现在线更新
- C#实现在线更新系统
- web程序在线更新实现
- 安卓APP采用观察者模式实现检测版本更新
- 安卓APP采用观察者模式实现检测版本更新
- 解析安卓erikwt的ListView下拉更新的实现
- Eclipse实现安卓客户端的版本更新
- 安卓图片在线浏览
- 安卓在线看源码
- 安卓下载更新
- CTF训练营-牛刀小试-ASCII码而已
- Shiro 学习笔记(7)—— Shiro 集成 Web
- Wildcard Matching
- 局域网如何检测电脑是否遭受ARP攻击
- 【Android】详解7.0带来的新工具类:DiffUtil
- 安卓实现在线更新
- Java----socket网络编程
- IOI2014 假期(Holiday)
- TCP的三次握手(建立连接)和四次挥手(关闭连接)
- 第3周项目2-建设“顺序表”算法库
- 二叉树的遍历
- 高德地图显示在fragment中并添加按钮点击事件
- shell脚本----if(数字条件,字符串条件,字符串为空)
- Android基础动画