Skip to content

Commit

Permalink
新增只用下载功能的使用方法
Browse files Browse the repository at this point in the history
  • Loading branch information
vector committed Apr 10, 2018
1 parent c834b1f commit 47802d1
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
69 changes: 69 additions & 0 deletions java.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [自定义接口协议+自定义对话框+显示进度对话框](#自定义接口协议+自定义对话框+显示进度对话框)
* [静默下载](#静默下载)
* [静默下载+自定义对话框](#静默下载+自定义对话框)
* [只用下载功能](#只用下载功能)
* [实现HttpManager接口](#实现HttpManager接口)

### 默认接口协议
Expand Down Expand Up @@ -440,6 +441,74 @@ onFinish() 当返回 true :下载完自动跳到安装界面,false:则不
.show();
}

```
### 只用下载功能

```java

UpdateAppBean updateAppBean = new UpdateAppBean();

//设置 apk 的下载地址
updateAppBean.setApkFileUrl("https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk");

String path = "";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {
try {
path = getExternalCacheDir().getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(path)) {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
} else {
path = getCacheDir().getAbsolutePath();
}

//设置apk 的保存路径
updateAppBean.setTargetPath(path);
//实现网络接口,只实现下载就可以
updateAppBean.setHttpManager(new UpdateAppHttpUtil());

UpdateAppManager.download(this, updateAppBean, new DownloadService.DownloadCallback() {
@Override
public void onStart() {
HProgressDialogUtils.showHorizontalProgressDialog(JavaActivity.this, "下载进度", false);
Log.d(TAG, "onStart() called");
}

@Override
public void onProgress(float progress, long totalSize) {
HProgressDialogUtils.setProgress(Math.round(progress * 100));
Log.d(TAG, "onProgress() called with: progress = [" + progress + "], totalSize = [" + totalSize + "]");

}

@Override
public void setMax(long totalSize) {
Log.d(TAG, "setMax() called with: totalSize = [" + totalSize + "]");
}

@Override
public boolean onFinish(File file) {
HProgressDialogUtils.cancel();
Log.d(TAG, "onFinish() called with: file = [" + file.getAbsolutePath() + "]");
return true;
}

@Override
public void onError(String msg) {
HProgressDialogUtils.cancel();
Log.e(TAG, "onError() called with: msg = [" + msg + "]");
}

@Override
public boolean onInstallAppAndAppOnForeground(File file) {
Log.d(TAG, "onInstallAppAndAppOnForeground() called with: file = [" + file + "]");
return false;
}
});

```

### 实现HttpManager接口
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/java/com/vector/appupdatedemo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void onCreate() {
.init(this)
.debug(true, "okHttp")
.timeout(20 * 1000);

OkGo.getInstance().init(this);
}
}
69 changes: 69 additions & 0 deletions sample/src/main/java/com/vector/appupdatedemo/ui/JavaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Expand All @@ -34,6 +35,7 @@
import java.util.Map;

public class JavaActivity extends AppCompatActivity {
private static final String TAG = JavaActivity.class.getSimpleName();
private String mUpdateUrl = "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt";
private boolean isShowDownloadProgress;

Expand All @@ -48,6 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_default_silence));
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_default_silence_diy_dialog));
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_default), 0xffe94339);

DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_download));
}

@Override
Expand Down Expand Up @@ -534,4 +538,69 @@ public void onClick(DialogInterface dialog, int which) {
.show();
}

public void onlyDownload(View view) {
Log.d(TAG, "onlyDownload() called with: view = [" + view + "]");
UpdateAppBean updateAppBean = new UpdateAppBean();

//设置 apk 的下载地址
updateAppBean.setApkFileUrl("https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk");

String path = "";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {
try {
path = getExternalCacheDir().getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(path)) {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
} else {
path = getCacheDir().getAbsolutePath();
}

//设置apk 的保存路径
updateAppBean.setTargetPath(path);
//实现网络接口,只实现下载就可以
updateAppBean.setHttpManager(new UpdateAppHttpUtil());

UpdateAppManager.download(this, updateAppBean, new DownloadService.DownloadCallback() {
@Override
public void onStart() {
HProgressDialogUtils.showHorizontalProgressDialog(JavaActivity.this, "下载进度", false);
Log.d(TAG, "onStart() called");
}

@Override
public void onProgress(float progress, long totalSize) {
HProgressDialogUtils.setProgress(Math.round(progress * 100));
Log.d(TAG, "onProgress() called with: progress = [" + progress + "], totalSize = [" + totalSize + "]");

}

@Override
public void setMax(long totalSize) {
Log.d(TAG, "setMax() called with: totalSize = [" + totalSize + "]");
}

@Override
public boolean onFinish(File file) {
HProgressDialogUtils.cancel();
Log.d(TAG, "onFinish() called with: file = [" + file.getAbsolutePath() + "]");
return true;
}

@Override
public void onError(String msg) {
HProgressDialogUtils.cancel();
Log.e(TAG, "onError() called with: msg = [" + msg + "]");
}

@Override
public boolean onInstallAppAndAppOnForeground(File file) {
Log.d(TAG, "onInstallAppAndAppOnForeground() called with: file = [" + file + "]");
return false;
}
});
}
}
9 changes: 9 additions & 0 deletions sample/src/main/res/layout/activity_java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,14 @@
android:text="更新App(静默下载+自定义对话框)"
/>

<Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="onlyDownload"
android:text="只用下载功能"
/>

</LinearLayout>
</ScrollView>
4 changes: 2 additions & 2 deletions update-app-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ dependencies {

compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

compile 'com.qianwen:update-app:'+rootProject.ext.update_app_version
// compile 'com.qianwen:update-app:'+rootProject.ext.update_app_version

// compile project(':update-app')
compile project(':update-app')
}
//添加
publish {
Expand Down

0 comments on commit 47802d1

Please sign in to comment.