Android自动在线升级

首先说一下运行环境,android2.3测试(主线程http请求),tomcat7.0作为服务器

tomca配置:

在webapps下的root文件夹下放了两个文件,一个为apk版本信息txt文件(当然也可以在servlet中返回json),一个为新的apk的文件(v2.0)

//install.txt内容
1
versioncode=2 2 url=http://192.168.1.131/android_apk_install.apk

 

android配置:

首先,机子运行的是apk为v1.0版本,服务器的为v2.0版本

权限配置(网络和sd卡写入):

1 <uses-permission android:name="android.permission.INTERNET" />
2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

下面几个方法为网络的几个工具方法

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 Hashtable<String, String> getAPKInstallContent(String path) {
 2         InputStream inputStream = getDataByHttpGet(path);
 3         Hashtable<String, String> hashtable = new Hashtable<String, String>();
 4         Properties properties = new Properties();
 5         if (inputStream != null) {
 6             try {
 7                 properties.load(inputStream);
 8                 Set<Entry<Object, Object>> set = properties.entrySet();
 9                 for (Entry<Object, Object> entry : set) {
10                     hashtable.put(entry.getKey().toString(), entry.getValue()
11                             .toString());
12                 }
13             } catch (IOException e) {
14                 e.printStackTrace();
15             } finally {
16                 if (inputStream != null) {
17                     try {
18                         inputStream.close();
19                     } catch (IOException e) {
20                         e.printStackTrace();
21                     }
22                 }
23             }
24         }
25         return hashtable;
26     }
复制代码

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 Hashtable<String, String> hashtable = 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();
        }
    }
}

posted on 2013-03-07 22:30  疯狂的石头陈  阅读(350)  评论(0编辑  收藏  举报

导航