手机安全卫士开发系列(5)——自动更新
2013-08-21 22:25
471 查看
新建一个MainActivity
配置mainfest文件
开启一个线程下载文件
自动安装apk文件
新启一个线程用来延时两秒中(这样就可以看到闪屏界面)
延时后通知主线程开始下面的事
资源下载:http://download.csdn.net/detail/lxq_xsyu/5989881
package com.meritit.mobiesafe.ui; import com.meritit.mobiesafe.R; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); } }
配置mainfest文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.meritit.mobiesafe" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.meritit.mobiesafe" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner" /> <activity android:name=".ui.SplashActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ui.MainActivity" android:label="@string/main_screen"></activity> </application> </manifest>新建一个DownLoadFileTask类用于下载文件
package com.meritit.mobiesafe.engine; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.ProgressDialog; public class DownLoadFileTask { /** * * @param path 服务器文件路径 * @param filepath 本地文件路径 * @return 本地文件对象 * @throws Exception */ public static File getFile(String path,String filepath,ProgressDialog pd) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); if(conn.getResponseCode() == 200){ int total = conn.getContentLength(); pd.setMax(total); InputStream is = conn.getInputStream(); File file = new File(filepath); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = 0; int process = 0; while((len = is.read(buffer))!=-1){ fos.write(buffer, 0, len); process +=len; pd.setProgress(process); Thread.sleep(50); } fos.flush(); fos.close(); is.close(); return file; } return null; } }
开启一个线程下载文件
private class DownLoadFileThreadTask implements Runnable { private String path; // 服务器路径 private String filepath; // 本地文件路径 public DownLoadFileThreadTask(String path, String filepath) { this.path = path; this.filepath = filepath; } public void run() { try { File file = DownLoadFileTask.getFile(path, filepath,pd); Log.i(TAG,"下载成功"); pd.dismiss(); install(file); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "下载文件失败", 0).show(); pd.dismiss(); loadMainUI(); } } }
自动安装apk文件
/** * 安装apk * @param file */ private void install(File file){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); finish(); startActivity(intent); }
新启一个线程用来延时两秒中(这样就可以看到闪屏界面)
new Thread(){ public void run() { super.run(); try { sleep(2000); handler.sendEmptyMessage(0); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start();
延时后通知主线程开始下面的事
private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { super.handleMessage(msg); //判读服务器的版本号和客户端的版本号是否相同 if(isNeedUpdate(versiontext)){ Log.i(TAG, "弹出升级对话框"); showUpdateDialog(); } }; };
资源下载:http://download.csdn.net/detail/lxq_xsyu/5989881
相关文章推荐
- 手机安全卫士开发系列(5)——自动更新
- 手机安全卫士开发系列(5)——自动更新
- [置顶] 手机安全卫士开发系列(4)——显示更新对话框
- 手机安全卫士开发系列(4)——显示更新对话框
- 手机安全卫士开发系列(4)——显示更新对话框
- 手机安全卫士开发系列(3)——获取更新的服务器配置
- 手机安全卫士开发系列(3)——获取更新的服务器配置
- IBatisNet 开发指南系列文章更新
- MOno C# 开发自动更新的时候出现签名冲突 的问题
- 【Himi之Cocos2d-iOS游戏开发】系列博文26篇整合以及Android书籍的源码和样章▃11月10日更新第二十六章
- Android应用开发之自动更新不用愁
- 手机安全卫士开发系列(1)——功能列表
- 从零开发微信公众号系列 【三】接收和自动回复消息
- Android Studio系列教程三-- 更新studio、设置Eclipse风格的快捷键、(自动)导包
- Android学习系列(3)--App自动更新之自定义进度视图和内部存储
- Provisioning Services 7.8 入门系列教程之十 通过类自动更新虚拟磁盘
- IBatisNet 开发指南系列文章更新
- iOS开发技巧(系列十五:autolayout自动布局)
- Cordova webapp实战开发:(5)如何写一个Andorid下自动更新的插件?
- iOS开发系列文章(持续更新……)