android版本更新

public void getVersion(String type) {        try {            PackageManager packageManager = context.getPackageManager();            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);            getUpdateInfo(packageInfo.versionName, type);            Log.e("version", packageInfo.versionName);        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }    }    public void getUpdateInfo(final String verId, final String type) {        new AsyncTask() {

@Override

protected Void doInBackground(Void... params) {

try {

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(Void aVoid) {

super.onPostExecute(aVoid);

if (!StringUtils.equals(verId, "")) {

new AlertDialog.Builder(context).setTitle("更新提示").

setMessage("快来更新最新版本吧")

.setPositiveButton("稍后再说", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

})

.setNegativeButton("更新", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

downFile("http://apk.r1.market.hiapk.com/data/upload/marketClient/HiMarket7.2.0.81_1468985255944.apk");

}

}).show();

} else {

if (!StringUtils.equals(type, "auto")) {

Toast.makeText(context, "暂无更新", Toast.LENGTH_SHORT).show();

}

}

}

}.execute();

}

private void downFile(final String url) {

m_progressDlg = new ProgressDialog(this);

m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确

m_progressDlg.setIndeterminate(false);

m_progressDlg.show();

new Thread() {

public void run() {

HttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(url);

HttpResponse response;

try {

response = client.execute(get);

HttpEntity entity = response.getEntity();

long length = entity.getContentLength();

m_progressDlg.setMax((int) length);//设置进度条的最大值

InputStream is = entity.getContent();

FileOutputStream fileOutputStream = null;

if (is != null) {

File file = new File(

Environment.getExternalStorageDirectory(),

m_appNameStr);

fileOutputStream = new FileOutputStream(file);

byte[] buf = new byte[1024];

int ch = -1;

int count = 0;

while ((ch = is.read(buf)) != -1) {

fileOutputStream.write(buf, 0, ch);

count += ch;

if (length > 0) {

m_progressDlg.setProgress(count);

}

}

}

fileOutputStream.flush();

if (fileOutputStream != null) {

fileOutputStream.close();

}

down();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}.start();

}

private void down() {

m_mainHandler.post(new Runnable() {

public void run() {

m_progressDlg.cancel();

update();

}

});

}

void update() {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(new File(Environment

.getExternalStorageDirectory(), m_appNameStr)),

"application/vnd.android.package-archive");

startActivity(intent);

}

你可能感兴趣的:(android版本更新)