<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.CheckActivity">
<TextView
android:padding="@dimen/s10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/s25dp"
android:id="@+id/tv_versionName"
android:text="版本信息"
/>
<Button
android:id="@+id/checkVersion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_versionName"
android:text="检测更新"
/>
</RelativeLayout>
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.yikezhong.R;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class CheckActivity extends AppCompatActivity {
private Button checkVersion;
private static final int DOWN_ERROR = 505;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
checkVersion =findViewById(R.id.checkVersion);
tv =findViewById(R.id.tv_versionName);
try {
tv.setText("版本名"+getVersionName());
} catch (Exception e) {
e.printStackTrace();
}
checkVersion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (getVersionCode() < 3) {
showDialogUpdate();
}else{
Toast.makeText(CheckActivity.this,"当前已经是最新的版本",Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 提示版本更新的对话框
*/
private void showDialogUpdate() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("版本升级").
setIcon(R.mipmap.ic_launcher).
setMessage("发现新版本!请及时更新").
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
loadNewVersionProgress();
}
}).
setNegativeButton("取消", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
/**
* 下载新版本程序
*/
private void loadNewVersionProgress() {
final String uri="https://www.zhaoapi.cn/version/app.apk";
final ProgressDialog pd;
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
new Thread(){
@Override
public void run() {
try {
File file = getFileFromServer(uri, pd);
sleep(3000);
installApk(file);
pd.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}}.start();
}
/**
* 安装apk
*/
protected void installApk(File file) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
/**
* 从服务器获取apk文件的代码
* 传入网址uri,进度条对象即可获得一个File文件
* (要在子线程中执行哦)
*/
public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
long time= System.currentTimeMillis();
File file = new File(Environment.getExternalStorageDirectory(), time+"updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len ;
int total=0;
while((len =bis.read(buffer))!=-1){
fos.write(buffer, 0, len);
total+= len;
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
}
else{
return null;
}
}
private String getVersionName() throws Exception {
PackageManager packageManager = getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
return packInfo.versionName;
}
private int getVersionCode() {
try {
PackageManager packageManager = getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
return packInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
}