Phonegap 安卓的自动升级与更新。当版本为4.0的时候
清单文件中:
- <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
js文件:
- [{"verName":"xinjiangQJ","verCode":2}]
主窗口MainActivity中的代码:
- package fx.qj.cn;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.Window;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.net.Uri;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import org.apache.cordova.*;
- public class MainActivity extends DroidGap {
- private long exitTime = 0;
- //自动检查更新开始
- private String localApkName = "XJQJW。apk";//存在于本地的apk名字
- private String packageName = "fx.qj.cn";//本应用包名,用于获取本地版本号
- private int serverVersion = 0;//服务端版本号
- private int localVersion = 0;//本地版本号
- private ProgressDialog pBar = null;//“正在下载”对话框
- @Override
- public void onCreate(Bundle savedInstanceState) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- super.onCreate(savedInstanceState);
- super.init();
- android.webkit.WebSettings settings = super.appView.getSettings();
- String appCachePath = this.getCacheDir().getAbsolutePath();
- settings.setAppCachePath(appCachePath);
- settings.setAllowFileAccess(true);
- settings.setAppCacheEnabled(true);
- super.setIntegerProperty("splashscreen",R.drawable.startup_bg1);
- super.loadUrl("file:///android_asset/www/index.html",5000);
- checkUpdate();//自动检查更新
- }
- //双击退出
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- exit();
- return false;
- }
- return super.onKeyDown(keyCode, event);
- }
- public void exit() {
- if ((System.currentTimeMillis() - exitTime) > 2000) {
- Toast.makeText(getApplicationContext(), "再按一次退出程序",
- Toast.LENGTH_SHORT).show();
- exitTime = System.currentTimeMillis();
- } else {
- finish();
- System.exit(0);
- }
- }
- //自动更新
- private void checkUpdate() {
- localVersion = getLocalVersion();
- new Thread() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- super.run();
- URL url_2;
- String str = "";
- int apkVersion = 0;
- String apkPath = "";
- try {
- String path=getResources().getString(R.string.url_server);
- url_2 = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url_2
- .openConnection();
- conn.setConnectTimeout(5000);
- InputStream is = conn.getInputStream();
- byte[] json = new byte[1024];
- is.read(json);
- str = new String(json, "utf-8");
- JSONArray array = new JSONArray(str);
- JSONObject item = array.getJSONObject(0);
- apkVersion = item.getInt("verCode");
- String apkpath=getResources().getString(R.string.url_server);
- apkPath = apkpath;
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Bundle data = new Bundle();
- data.putInt("apkVersion", apkVersion);
- data.putString("apkPath", apkPath);
- Message msg = new Message();
- msg.what = 1;
- msg.setData(data);
- hander.sendMessage(msg);
- }
- }.start();
- }
- private int getLocalVersion(){
- int version=-1;
- try {
- version = this.getPackageManager().getPackageInfo(packageName, 0).versionCode;
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Log.e("TAG",e.getMessage());
- }
- return version;
- }
- private Handler hander = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case 1:
- Bundle b = msg.getData();
- serverVersion = b.getInt("apkVersion");
- final String apkPath = b.getString("apkPath");
- Log.v("apkVersion", serverVersion+"");
- if(localVersion<serverVersion){
- AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
- alert.setTitle("软件更新").setMessage("发现新版本,建议立即更新使用.").setPositiveButton("更新", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 确定更新
- pBar = new ProgressDialog(MainActivity.this);
- pBar.setTitle("正在下载");
- pBar.setMessage("请稍后。。。");
- pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- String apkpath=getResources().getString(R.string.url_apk);
- downFile(apkpath);
- }
- }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- alert.create().show();
- }
- break;
- }
- }
- };
- public void downFile(final String url){
- pBar.show();
- new Thread(){
- public void run(){
- HttpClient client = new DefaultHttpClient();
- HttpGet get = new HttpGet(url);
- HttpResponse response;
- System.out.println(url);
- try {
- response = client.execute(get);
- HttpEntity entity = response.getEntity();
- long length = entity.getContentLength();
- InputStream is = entity.getContent();
- FileOutputStream fileOutputStream = null;
- if(is != null){
- File file = new File(Environment.getExternalStorageDirectory(),localApkName);
- fileOutputStream = new FileOutputStream(file);
- byte[] b = new byte[1024];
- int charb = -1;
- int count = 0;
- while((charb = is.read(b))!=-1){
- fileOutputStream.write(b, 0, charb);
- count += charb;
- }
- }
- fileOutputStream.flush();
- if(fileOutputStream!=null){
- fileOutputStream.close();
- }
- down();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }.start();
- }
- Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- pBar.cancel();
- update();
- }
- };
- public void down(){
- new Thread(){
- public void run(){
- Message message = handler.obtainMessage();
- handler.sendMessage(message);
- }
- }.start();
- }
- public void update(){
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),localApkName))
- , "application/vnd.android.package-archive");
- startActivity(intent);
- }
- }
把http请求【HttpURLConnection conn = (HttpURLConnection) url_2.openConnection();】放到了主线程中来运行,
报错android.os.NetworkOnMainThreadException
直接翻译就是:网络工作在主线程中的异常;网上一查发现是android中规定网络请求等耗时操作是不允许在主线程中出现的,需要另起线程,线程若有数据需要与主线程交互就可以用handler来接收。
参考资料连接: http://my.eoe.cn/1222037/archive/21064.html
Phonegap 安卓的自动升级与更新。当版本为4.0的时候的更多相关文章
- [转]Android 应用的自动升级、更新模块的实现
本文转自:http://www.oschina.net/question/163910_28462 我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Andro ...
- Android应用的自动升级、更新模块的实现(转)
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下.首先给出界面效果: ...
- Android应用的自动升级、更新模块的实现
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下.首先给出界面效果: ...
- sublime text3入门笔记以及屏蔽sublime自动升级检测更新
两个月前学习python的时候,有人推荐这个程序员最好用的编辑器,我下载了之后,发现比notepad++要好用很多,目前来说,网上成熟的版本是sublime text2简体中文版,插件也是很兼容,我用 ...
- 安卓App自动升级
procedure _InstallApk(Apk: string); var LFile: JFile; LIntent: JIntent; begin LFile := TJFile.JavaCl ...
- WebMisSharp升级说明,最新版本1.6.0
尊敬的C3 AM.C3 FX.WebMisSharp用户您好: 非常感谢长期来您对WebMisSharp系列产品的支持,您的使用和反馈是我们进步的最大动力.在你们的帮助下我们又向前迈进了一步,我们功能 ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.1build803)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.1.build803 编译日期:2014-08-06 ====================================== 二. ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2build851)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build851 编译日期:2015-06-26 ====================================== 二. ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2.build124)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build124 编译日期:2014-08-19 ====================================== 二. ...
随机推荐
- BZOJ 1014 火星人prefix
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- 使用Flexible 实现手淘H5 页面的终端适配学习
Amfe阿里无线前端团队双11技术连载之际,一个实战案例来展示多终端适配. Device metrics 1.0 mdpi 2.0 xhdpi 3.0xxhdpi(iphone 6 plus) 手淘h ...
- Unity3D 3D横版跑酷 跳跃
Unity3d 跑酷动画的控制 首先给个图吧, 我们跑酷里面需要动画的,今天说一下动画的知识! 1.导入骨骼动画模型文件之后,如果使用之前版本的unity的播放动画的方式,需要设置AnimationT ...
- codeforces Upgrading Array
思路:对于每个数分解质因子然后记录每一个质因子的个数,对与在b中出现的质因子就减去1,否则加1,求出总的,然后从后面一次对它们的最大公约数,然后判断除以最大公约数之后,改变量是不是变化,求最大值,变化 ...
- yum命令常见方法
yum check-update 检查可更新的所有软件包 yum update 下载更新系统已安装的所有软件包 yum upgrade 大规模的版本升级,与yum update不同的是,连旧的淘汰的包 ...
- Apache CXF多个远程拒绝服务漏洞(CVE-2013-2160)
漏洞版本: Apache Group CXF <= 2.5.10 Apache Group CXF 2.7.4 Apache Group CXF 2.6.7 漏洞描述: BUGTRAQ ID: ...
- weblogic启动报错之未修改hosts产生错误
报错如下: Enter username to boot WebLogic server:weblogic Enter password to boot WebLogic server: <Ju ...
- HDOJ 2056 Rectangles
Problem Description Given two rectangles and the coordinates of two points on the diagonals of each ...
- Jack Straws(判断线段是否相交 + 并查集)
/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840 题意: 判断线段 ...
- Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP
D. Little Pony and Harmony Chest Princess Twilight went to Celestia and Luna's old castle to resea ...