Skip to content

Commit

Permalink
添加动态获取下载地址
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Jun 30, 2021
1 parent 4a040d0 commit 9eb4844
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 10 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.github.maning0303:MNProgressHUD:V2.0.0X'

implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation project(':library_update')

Expand Down
87 changes: 80 additions & 7 deletions app/src/main/java/com/maning/mnupdateapk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.maning.mndialoglibrary.MProgressDialog;
import com.maning.mndialoglibrary.MToast;
import com.maning.mnupdateapk.bean.PgyerAppCheckResultBean;
import com.maning.mnupdateapk.cons.Constants;
import com.maning.mnupdateapk.utils.PermissionUtils;
import com.maning.updatelibrary.InstallUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Expand All @@ -33,6 +50,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private Button btnOther;
private InstallUtils.DownloadCallBack downloadCallBack;
private String apkDownloadPath;
private Button mGetDownloadUrl;


@Override
Expand All @@ -41,25 +59,68 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
context = this;

initViews();
initView();

initCallBack();

}


private void initViews() {
private void initView() {
tv_progress = (TextView) findViewById(R.id.tv_progress);
tv_info = (TextView) findViewById(R.id.tv_info);
btnDownload = (Button) findViewById(R.id.btnDownload);
btnCancle = (Button) findViewById(R.id.btnCancle);
btnDownloadBrowser = (Button) findViewById(R.id.btnDownloadBrowser);
btnOther = (Button) findViewById(R.id.btnOther);

mGetDownloadUrl = (Button) findViewById(R.id.getDownloadUrl);
btnDownload.setOnClickListener(this);
btnCancle.setOnClickListener(this);
btnDownloadBrowser.setOnClickListener(this);
btnOther.setOnClickListener(this);
mGetDownloadUrl.setOnClickListener(this);
}

private void getAppUpdateInfo() {
MProgressDialog.showProgress(this);
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://www.pgyer.com/apiv2/app/check";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
MProgressDialog.dismissProgress();
if(!TextUtils.isEmpty(response)){
PgyerAppCheckResultBean result = new Gson().fromJson(response, PgyerAppCheckResultBean.class);
if (result != null && result.getCode() == 0 && result.getData() != null) {
PgyerAppCheckResultBean.Data data = result.getData();
String downloadURL = data.getDownloadURL();
Log.e("======","downloadURL:"+downloadURL);
Constants.APK_URL = downloadURL;
MToast.makeTextShort("获取地址成功");
return;
}
}
MToast.makeTextShort("获取信息错误错误");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
MProgressDialog.dismissProgress();
MToast.makeTextShort("错误:"+error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("_api_key", "c193c7301fd75ae5a771d8923df6300a");
params.put("appKey", "bfed5049f2b2c0ce048fd62b015956ac");
params.put("buildVersion", "1.0.0");
return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}

@Override
Expand Down Expand Up @@ -194,10 +255,18 @@ public void onClick(View v) {
startActivity(new Intent(this, OtherActivity.class));
break;
case R.id.btnDownloadBrowser:
if(TextUtils.isEmpty(Constants.APK_URL)){
Toast.makeText(context,"请先获取最新的下载地址",Toast.LENGTH_SHORT).show();
return;
}
//通过浏览器去下载APK
InstallUtils.installAPKWithBrower(this, Constants.APK_URL);
break;
case R.id.btnDownload:
if(TextUtils.isEmpty(Constants.APK_URL)){
Toast.makeText(context,"请先获取最新的下载地址",Toast.LENGTH_SHORT).show();
return;
}
//申请SD卡权限
if (!PermissionUtils.isGrantSDCardReadPermission(this)) {
PermissionUtils.requestSDCardReadPermission(this, 100);
Expand All @@ -213,6 +282,9 @@ public void onClick(View v) {
.startDownload();
}
break;
case R.id.getDownloadUrl:
getAppUpdateInfo();
break;
default:
break;
}
Expand All @@ -237,4 +309,5 @@ public long getFileSizes(File f) {
return 0;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.maning.mnupdateapk.bean;

public class PgyerAppCheckResultBean {


private int code;
private String message;
private Data data;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

public class Data {

private String buildBuildVersion;
private String forceUpdateVersion;
private String forceUpdateVersionNo;
private boolean needForceUpdate;
private String downloadURL;
private boolean buildHaveNewVersion;
private String buildVersionNo;
private String buildVersion;
private String buildUpdateDescription;
private String appKey;
private String buildKey;
private String buildName;
private String buildIcon;
private String buildFileKey;
private long buildFileSize;

public String getBuildBuildVersion() {
return buildBuildVersion;
}

public void setBuildBuildVersion(String buildBuildVersion) {
this.buildBuildVersion = buildBuildVersion;
}

public String getForceUpdateVersion() {
return forceUpdateVersion;
}

public void setForceUpdateVersion(String forceUpdateVersion) {
this.forceUpdateVersion = forceUpdateVersion;
}

public String getForceUpdateVersionNo() {
return forceUpdateVersionNo;
}

public void setForceUpdateVersionNo(String forceUpdateVersionNo) {
this.forceUpdateVersionNo = forceUpdateVersionNo;
}

public boolean isNeedForceUpdate() {
return needForceUpdate;
}

public void setNeedForceUpdate(boolean needForceUpdate) {
this.needForceUpdate = needForceUpdate;
}

public String getDownloadURL() {
return downloadURL;
}

public void setDownloadURL(String downloadURL) {
this.downloadURL = downloadURL;
}

public boolean isBuildHaveNewVersion() {
return buildHaveNewVersion;
}

public void setBuildHaveNewVersion(boolean buildHaveNewVersion) {
this.buildHaveNewVersion = buildHaveNewVersion;
}

public String getBuildVersionNo() {
return buildVersionNo;
}

public void setBuildVersionNo(String buildVersionNo) {
this.buildVersionNo = buildVersionNo;
}

public String getBuildVersion() {
return buildVersion;
}

public void setBuildVersion(String buildVersion) {
this.buildVersion = buildVersion;
}

public String getBuildUpdateDescription() {
return buildUpdateDescription;
}

public void setBuildUpdateDescription(String buildUpdateDescription) {
this.buildUpdateDescription = buildUpdateDescription;
}

public String getAppKey() {
return appKey;
}

public void setAppKey(String appKey) {
this.appKey = appKey;
}

public String getBuildKey() {
return buildKey;
}

public void setBuildKey(String buildKey) {
this.buildKey = buildKey;
}

public String getBuildName() {
return buildName;
}

public void setBuildName(String buildName) {
this.buildName = buildName;
}

public String getBuildIcon() {
return buildIcon;
}

public void setBuildIcon(String buildIcon) {
this.buildIcon = buildIcon;
}

public String getBuildFileKey() {
return buildFileKey;
}

public void setBuildFileKey(String buildFileKey) {
this.buildFileKey = buildFileKey;
}

public long getBuildFileSize() {
return buildFileSize;
}

public void setBuildFileSize(long buildFileSize) {
this.buildFileSize = buildFileSize;
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maning.mnupdateapk;
package com.maning.mnupdateapk.cons;

import android.os.Environment;

Expand All @@ -13,7 +13,7 @@
*/
public class Constants {

public static final String APK_URL = "https://www.pgyer.com/app/installUpdate/8a6470a9b1cb5efab0fbcabb27fda49a?sig=w6kLobcll%2FfKwM6VjFrtdyy9LGbjobOJGfXxoinLCUgSB6eonijemoYTv2LVhYx8&forceHttps=";
public static String APK_URL = "";


public static final String APK_SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MNUpdateAPK/update.apk";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maning.mnupdateapk;
package com.maning.mnupdateapk.utils;

import android.Manifest;
import android.app.Activity;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
android:orientation="vertical"
android:padding="10dp">

<Button
android:id="@+id/getDownloadUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:text="获取最新的下载地址"
android:textColor="#FFFFFF" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 9eb4844

Please sign in to comment.