**
* Android
**
===========================
一:配置:
build:
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.okio:okio:1.7.0'
这个是我的版本,根据自己项目调整。
**AndroidManifest.xml:**
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
二:代码:
首先是更新实体Bean-UpdateBean:
package com.ma.update;
public class UpdateBean {
private String title;
private String message;
private int versionCode;
private String versionName;
private String url;
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getVersionCode() {
return versionCode;
}
public void setVersionCode(int versionCode) {
this.versionCode = versionCode;
}
}
网络工具类:
/**
* 下载apk
*/
public static void httpDownLoadApk(String url, Context context, Map BodyParams, okhttp3.Callback callback) {
// 打印日志
getHttpUrl(url, BodyParams);
OkHttpClient mOkHttpClient = builder.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(callback);
}
更新对话框:
**package com.ma.update;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by mjm on 2017/5/17.
* 更新对话框
*/
public class UpdateDialog extends Dialog implements View.OnClickListener {
private UpdateDialogOperate aDialogOperate; // 操作接口
private Context context;
private TextView ver;
private TextView content;
private Button update_ok;
private Button upadte_cancle;
private UpdateBean upadtebean;
private TextView tip;
public UpdateDialog(Context context) {
super(context, R.style.common_dialog);
this.context = context;
this.setContentView(R.layout.dialog_update);
ver = (TextView) findViewById(R.id.update_ver);
content = (TextView) findViewById(R.id.update_content);
update_ok = (Button) findViewById(R.id.update_btn_ok);
upadte_cancle = (Button) findViewById(R.id.update_btn_cancle);
tip = (TextView) findViewById(R.id.update_tip);
update_ok.setOnClickListener(this);
upadte_cancle.setOnClickListener(this);
}
public void setData(UpdateBean upadtebean, boolean flag, UpdateDialogOperate aDialogOperate) {
this.aDialogOperate = aDialogOperate;
this.upadtebean = upadtebean;
upadte_cancle.setVisibility(View.VISIBLE);
tip.setVisibility(View.GONE);
//关闭以后再说。。。
upadte_cancle.setVisibility(View.GONE);
update_ok.setVisibility(View.VISIBLE);
ver.setText(upadtebean.getVersionName());
content.setText(upadtebean.getMessage());
this.setCancelable(false);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.update_btn_cancle:
aDialogOperate.executeCancel("");
break;
case R.id.update_btn_ok:
aDialogOperate.executeCommit("");
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}**
更新代码:
public static int UpdateVersion(final Context context, final UpdateBean updateBean) {
String packageName = context.getPackageName();
int nowCode = getVersionCode(context);//手机端的版本
int newCode = updateBean.getVersionCode();
if (nowCode < newCode) {//小于最新版本号
checkPermission(context, updateBean);
} else {
Log.e("MA", "已经是最新版本");
// ToastUtils.showMessage("已经是最新的版本");
}
return 0;
}
public static void checkPermission(final Context context, final UpdateBean updateBean) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions((MainActivity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1000);
return;
} else {
showUpdateDialog(context, updateBean);
//
}
}
public static void showUpdateDialog(final Context context, final UpdateBean updateBean) {
final UpdateDialog updateDialog = new UpdateDialog(context);
updateDialog.setData(updateBean, true, new UpdateDialogOperate() {
@Override
public void executeCancel(String text) {
updateDialog.cancel();
}
@Override
public void executeCommit(String text) {
downFile(updateBean.getUrl(), context);
updateDialog.cancel();
}
});
updateDialog.show();
}
适配7.0安装:
public static void installApk(File file, Context context) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT > 23) {
Uri apkUri =
FileProvider.getUriForFile(context, "**包名**.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
AndroidManifase.xml:
"android.support.v4.content.FileProvider"
android:authorities="包名".fileprovider"
android:exported="false"
android:grantUriPermissions="true">
"
android:resource=" @xml/filepaths" />
Demo下载
**