码迷,mamicode.com
首页 > 移动开发 > 详细

安卓实现在线更新

时间:2016-04-26 20:37:40      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

////////////////////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下载不需要积分哦



安卓实现在线更新

标签:

原文地址:http://blog.csdn.net/xbw12138/article/details/51245455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!