Android-app automatic update summary (adapted to 9-0), talk about the principle of Android message mechanism

Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.REQUEST_INSTALL_PACKAGES})
protected void checkUpdate() { showLoadingDialog("Checking for updates..."); version = AppUpdateUtil.getAppVersionCode(this);//Check the current version number // Call 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, go to download apk file, here is the method to download 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
* @return App version code
*/
public static int getAppVersionCode(Context context) { return getAppVersionCode(context, context.getPackageName()); }

2.4 IntentUtil:

public class IntentUtil {

/**
* Get the intent to install App (support 7.0)
*
* @param context
* @param filePath
* @return
*/
public static Intent getInstallAppIntent(Context context, String filePath) { //Local path of apk file File apkfile = new File(filePath); if (!apkfile.exists()) { return null; } Intent intent = new Intent(Intent.ACTION_VIEW); Uri contentUri = FileUtil.getUriForFile(context, apkfile); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {








learning sharing

In the current era of information sharing, many resources can be found on the Internet, it just depends on whether you are willing to find it or how to find it, right?

It’s not that many friends don’t have information. Most of them have tens or hundreds of G, but they are disorganized. I don’t know how to read them, or even forget them after reading

If you feel that the information you find on the Internet is very messy and unsystematic, I will also share a set with you. It is more systematic, and I usually study it myself.

The latest tens of thousands of pages of Dachang interview questions in 2020

Seven modules of learning materials: such as NDK module development, Android framework architecture...

Only systematic and directional learning can quickly improve one's skills within a period of time.

This system of study notes is suitable for the crowd:
First, the learning knowledge is relatively fragmented, and there is no reasonable learning route and advanced direction.
Second, after several years of development, I don't know how to advance further, and I am quite confused.
Third, at the right age, I don’t know how to develop in the future, whether to transform management or strengthen technical research. If you need it, I just have a reason here, don't come and get it! Maybe it can change your current state!

This system of study notes is suitable for the crowd:
First, the learning knowledge is relatively fragmented, and there is no reasonable learning route and advanced direction.
Second, after several years of development, I don't know how to advance further, and I am quite confused.
Third, at the right age, I don’t know how to develop in the future, whether to transform management or strengthen technical research. If you need it, I just have a reason here, don't come and get it! Maybe it can change your current state!
Due to the large content of the article and the limited space, some undisplayed content will be displayed in the form of screenshots. If you need to get a complete document, click my GitHub to get it for free.

Guess you like

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