更新管理类
public class UploadManage {
private Context mcontext;
private String saveFilePath = "/sdcard/updataApk/";
private String saveFileNmae = saveFilePath + "apkName.apk";
private boolean canceFlag = false;
AlertDialog alertDialog;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
installApkFile(mcontext,saveFilePath);
break;
case 2:
break;
}
}
};
public void showDialog(final UploadAppBean.ResultBean resultBean) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);
dialog.setTitle("发现新版本" + resultBean.getVersionName());
dialog.setMessage(resultBean.getDesc());
dialog.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
downloadApk(resultBean.getApkUrl());
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (resultBean.isFlag()) {
Toast.makeText(mcontext, "close", Toast.LENGTH_SHORT).show();
} else {
dialogInterface.dismiss();
}
}
});
dialog.create().show();
}
public UploadManage(Context context) {
this.mcontext = context;
}
public void downloadApk(final String apkUrl) {
new Thread() {
@Override
public void run() {
try {
URL url = new URL(apkUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength();
InputStream is = connection.getInputStream();
File file = new File(saveFilePath);
if (!file.exists()) {
file.mkdir();
}
String apkFile = saveFileNmae;
File file1 = new File(apkFile);
FileOutputStream fos = new FileOutputStream(file1);
int count = 0;
byte[] bytes = new byte[1024];
int l = 0;
while ((l = is.read(bytes)) != -1) {
count += l;
fos.write(bytes, 0, l);
fos.flush();
Message message = new Message();
message.what = 2;
handler.sendMessage(message);
}
handler.sendEmptyMessage(1);
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public static void installApkFile(Context context ,String mSavePath) {
Log.d("cx","+++++++++++++++++++");
File apkfile = new File(mSavePath, "apkName.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (!apkfile.exists()) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
String ss = "com.example.updataapp" + ".fileprovider";
Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), ss, apkfile);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} catch (Exception e) {
e.printStackTrace();
}
} else {
intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
try {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
public long getAppVersionCode() {
long appVersionCode = 0;
try {
PackageInfo packageInfo = mcontext.getApplicationContext().getPackageManager().getPackageInfo(mcontext.getPackageName(), 0);
appVersionCode = packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appVersionCode;
}
public String getAppVersionName() {
String appVersionName = "";
try {
PackageInfo packageInfo = mcontext.getApplicationContext().getPackageManager().getPackageInfo(mcontext.getPackageName(), 0);
appVersionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appVersionName;
}
}
application标签下 添加provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.updataapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
新建res文件 下的 xml
<paths>
<!-- path="com.example.updataapp"== 包名 -->
<external-path
name="files_root"
path="com.example.updataapp" />
<!-- path="." == 所有路径 name="apkName.apk"== 安装的名称 -->
<external-path
name="apkName.apk"
path="." />
</paths>
判断更新
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UploadAppBean.ResultBean resultBean = setUpload();
uploadCode(resultBean);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
requestPermissions(new String[]{
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.READ_EXTERNAL_STORAGE"
}, 100);
}
}
public void uploadCode(UploadAppBean.ResultBean resultBean) {
UploadManage uploadManage = new UploadManage(this);
long code = uploadManage.getAppVersionCode();
String name = uploadManage.getAppVersionName();
if (code < resultBean.getVersionCode()) {
uploadManage.showDialog(resultBean);
}
}
public UploadAppBean.ResultBean setUpload() {
UploadAppBean.ResultBean resultBean = new UploadAppBean.ResultBean();
resultBean.setVersionCode(250);
resultBean.setVersionName("2.5.0");
resultBean.setApkUrl("http://10.161.9.95/upload/app-release.apk");
resultBean.setDesc("1.修改了bug,2.更新了我的页面手势解锁,3.添加了新品页面");
resultBean.setFlag(true);
return resultBean;
}
}