Android-app automatic update summary (adapted to 9-0) (1), android routing table location

1.2 Create a new file (path: res\xml\file_paths.xml):
1.3 (app的)build.gradle:

implementation "com.lzy.net:okgo:3.0.4"//okgo network request
implementation 'com.google.code.gson:gson:2.8.2'//gson
implementation "org.permissionsdispatcher:permissionsdispatcher:4.3.1" //Permission
annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.3.1"//Permission

2. Here is an example of clicking the button to update:
2.1 Core code:

private int version;
/* update progress bar */
private ProgressBar mProgress;
private AlertDialog mDownloadDialog;


//Click the button, check the permissions,,, check the update method
@NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.REQUEST_INSTALL_PACKAGES})
protected void checkUpdate() { showLoadingDialog(“Detect Updating... ”); version = AppUpdateUtil.getAppVersionCode(this);//Check the current version number // Call the method,,, the specific implementation of the interface, receive the passed parameters, and then adjust your own method, requestAppUpdate(version, new DataRequestListener() { @Override public void success(UpdateAppBean data) { // When the returned json, getStatus is 0, download the apk file, here is the method of downloading the apk file updateApp(data.getData().getApk_url()); }








@Override
public void fail(String msg) { // Returned json, when getStatus is 1, prompt: "Already the latest version!" SToast(msg); dismissLoadingDialog(); } }); }





//Check the version number, the first request (post),,, UpdateAppBean generates
private void requestAppUpdate(int version, final DataRequestListener listener) { OkGo.post(Const.HOST_URL + Const.UPDATEAPP).params(“version ", version).execute(new StringCallback() { @Override public void onSuccess(Response response) { Gson gson = new Gson(); UpdateAppBean updateAppBean = gson.fromJson(response.body(), UpdateAppBean.class); if ( updateAppBean.getStatus() == 0) { listener.success(updateAppBean); } else { listener.fail(updateAppBean.getMsg()); } }










@Override
public void onError(Response response) { listener.fail("Server connection failed"); dismissLoadingDialog(); } }); }




//If there is a new version, prompt that there is a new version, and then download the apk file
private void updateApp(String apk_url) { dismissLoadingDialog(); DialogUtils.getInstance().showDialog(this, "New version found, do you want to download the update?" , new DialogUtils.DialogListener() { @Override public void positiveButton() { downloadApp(apk_url); } }); }








//Download the apk file and jump (second request, get)
private void downloadApp(String apk_url) { OkGo.get(apk_url).tag(this).execute(new FileCallback() { @Override public void onSuccess(Response response) { String filePath = response.body().getAbsolutePath(); Intent intent = IntentUtil.getInstallAppIntent(mContext, filePath); // Tested here must use startactivity, not stratactivityforresult mContext.startActivity(intent); dismissLoadingDialog() ; mDownloadDialog.dismiss(); mDownloadDialog=null; }










@Override
public void downloadProgress(Progress progress) { // showDownloadDialog(); // mProgress.setProgress((int) (progress.fraction * 100)); if (mDownloadDialog == null) { // Construct the software download dialog box AlertDialog .Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("Updating"); // Add a progress bar to the download dialog final LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R .layout.item_progress, null); mProgress = (ProgressBar) v.findViewById(R.id.update_progress); builder.setView(v); mDownloadDialog = builder.create(); mDownloadDialog.setCancelable(false); mDownloadDialog.show( );














}
mProgress.setProgress((int) (progress.fraction * 100));
}
});
}

2.2 DataRequestListener:

public interface DataRequestListener { //Request success void success(T data); //Request failure void fail(String msg); }




Next is the tool class, from github, reference, https://github.com/vondear/RxTool

2.3 AppUpdateUtil:

/**
* Get App version code
*
* @param context context

How to become an Android architect?

Build your own knowledge framework, comprehensively improve your technical system, and delve deeper into the direction of the underlying source code.
Most technical people like to use mind maps to build their own knowledge system, which is clear at a glance. Here I would like to share with you a mainstream Android architect technology system of a big factory, which can be used to build your own knowledge framework, or to check for gaps;

Corresponding to this technical outline, I also compiled a complete set of video tutorials for Android senior architects, mainly for students with 3-5 years of Android development experience and above who need to learn and improve at the senior architect level. Click here to share for free on GitHub , hoping to help you break through the bottleneck and jump into a big factory;

Finally I must stress a few points:

1. Building a knowledge framework does not mean that you sort out the order of the knowledge you want to learn, and then you can copy and paste after reading it and understanding it. Most of them require you to understand the source code and principles yourself, and write it out by yourself.
2. When you are studying, you must read and practice several times to get a thorough understanding of the knowledge and take notes. These are very important! What level you reach in the end depends on how much knowledge you have digested.
3. In the end, your knowledge framework should be a complete technical system that takes into account both breadth and depth. Then you can reach the level of a senior architect after many times of actual project combat to accumulate experience.

You just need to fill yourself in this big framework, the annual salary of 40W must not be the end, the technology is endless

It is very important to watch and practice a few times to get a thorough understanding of the knowledge and take notes! What level you reach in the end depends on how much knowledge you have digested.
3. In the end, your knowledge framework should be a complete technical system that takes into account both breadth and depth. Then you can reach the level of a senior architect after many times of actual project combat to accumulate experience.

You just need to fill yourself in this big framework, the annual salary of 40W must not be the end, the technology is endless

Guess you like

Origin blog.csdn.net/m0_66264630/article/details/123002068