首先说一下运行环境,android2.3测试(主线程http请求),tomcat7.0作为服务器
tomca配置:
在webapps下的root文件夹下放了两个文件,一个为apk版本信息txt文件(当然也可以在servlet中返回json),一个为新的apk的文件(v2.0)
//install.txt内容 1 versioncode=22 url=http://192.168.1.131/android_apk_install.apk
android配置:
首先,机子运行的是apk为v1.0版本,服务器的为v2.0版本
权限配置(网络和sd卡写入):
12
下面几个方法为网络的几个工具方法
1.httpget请求,返回inputstream
1 public static InputStream getDataByHttpGet(String url) { 2 InputStream inputStream = null; 3 HttpGet httpGet = new HttpGet(url); 4 HttpClient httpClient = new DefaultHttpClient(); 5 try { 6 HttpResponse response = httpClient.execute(httpGet); 7 int code = response.getStatusLine().getStatusCode(); 8 if (code != 200) { 9 return null;10 }11 HttpEntity entity = response.getEntity();12 inputStream = entity.getContent();13 } catch (Exception e) {14 e.printStackTrace();15 }16 return inputStream;17 }
2.获取新apk的信息
1 public static HashtablegetAPKInstallContent(String path) { 2 InputStream inputStream = getDataByHttpGet(path); 3 Hashtable hashtable = new Hashtable (); 4 Properties properties = new Properties(); 5 if (inputStream != null) { 6 try { 7 properties.load(inputStream); 8 Set
3.下载apk
1 public static boolean downloadApk(String url) { 2 boolean flag = false; 3 InputStream inputStream = getDataByHttpGet(url); 4 File sd_file = Environment.getExternalStorageDirectory(); 5 File apk_file = new File(sd_file, "android_installapk.apk"); 6 OutputStream outputStream = null; 7 try { 8 outputStream = new FileOutputStream(apk_file); 9 byte data[] = new byte[1024];10 int len = 0;11 while ((len = inputStream.read(data)) != -1) {12 outputStream.write(data, 0, len);13 }14 flag = true;15 } catch (Exception e) {16 e.printStackTrace();17 }finally{18 if (inputStream != null) {19 try {20 inputStream.close();21 } catch (IOException e) {22 e.printStackTrace();23 }24 }25 if (outputStream != null) {26 try {27 outputStream.close();28 } catch (IOException e) {29 e.printStackTrace();30 }31 }32 }33 return flag;34 }
下面是Activity
public class MainActivity extends Activity { private String url = "http://192.168.1.131/install.txt"; private String packageName; private PackageManager packageManager; private PackageInfo packageInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Hashtablehashtable = HttpUtils.getApkInfo(url); packageName = getPackageName(); packageManager = getPackageManager(); try { packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); String str1 = hashtable.get("versioncode"); int str2 = packageInfo.versionCode; System.out.println("网络:" + str1 + "||" + "本机:" + str2); if (!String.valueOf(str2).equals(str1)) { new Builder(MainActivity.this).setTitle("温馨提示!") .setMessage("您的应用有新版本,是否需要下载安装?") .setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { boolean flag = HttpUtils.downloadApk(hashtable .get("url")); if (flag) { System.out.println("现在开始安装!"); Uri uri = Uri .fromFile(new File( "/mnt/sdcard/android_installapk.apk")); Intent intent = new Intent( Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.android.package-archive"); startActivity(intent); } else { System.out.println("没有进入安装,下载错误"); } } }).setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create().show(); } } catch (NameNotFoundException e) { e.printStackTrace(); } }}