新建一个MainActivity

package com.meritit.mobiesafe.ui;import com.meritit.mobiesafe.R;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected 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

更多相关文章

  1. Android(安卓)DatePicker 使用示例
  2. React-Native中用Android(安卓)Studio运行报Duplicate错
  3. android7.0适配权限问题
  4. 报错:ERROR: Your project path contains non-ASCII characters.
  5. Gradle 进阶学习
  6. 下载好自动安装
  7. android app 捕获全局异常,保存成文件
  8. 强力提高 android 4 编译效率(c,c++部分)
  9. FileUtil:文件处理工具类

随机推荐

  1. Android Http RequestCache缓存策略
  2. android (19)
  3. Fragments: The Solution to All of Andr
  4. Android7.0中文文档(API)-- Filter
  5. Android Studio矢量图Vector Drawable绘
  6. Android(安卓)ViewStub控件使用
  7. android 禁止横切屏的代码
  8. android代码格式化方法小结
  9. android 笔记-scrollview滑动阴影取消
  10. Android摄像头相关问题记录