package me.bell.downapp.util;
import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import java.io.File;
import me.bell.downapp.util.goodsViewPager.download.DownloadService;
/**
* @Info
* @Auth Bello
* @Time 18-4-11 下午7:59
* @Ver
*/
public class DownLoadUtils {
private static String[] requestPermissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.REQUEST_INSTALL_PACKAGES};
/**
* 调用外部浏览器打开url
*
* @param context
* @param url
*/
public static void startDownLoad(Context context, String appName, String url){
/*
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri uri = Uri.parse(url);
intent.setData(uri);
context.startActivity(intent);
*/
if(ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions((Activity) context, requestPermissions, 1);
} else {
onUpdate(context, appName, url);
}
}
/**
* 打开更新service
*/
private static void onUpdate(final Context context, final String appName, final String url) {
Intent intent = new Intent(context, DownloadService.class);
context.bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定service,获取进度显示到dialog上的progressBar
DownloadService.DownloadBinder binder = (DownloadService.DownloadBinder) service;
DownloadService myService = binder.getService();
//调用下载
myService.downApk(appName, url, "", new DownloadService.DownloadCallback() {
@Override
public void onPrepare() {
//开始
Toast.makeText(context, appName + " 下载开始", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgress(int progress) {
//下载进行中
// progressBar.setProgress(progress);
// progressValue.setText(progress + "/100%");
}
@Override
public void onComplete(File file) {
//下载完成
// Log.e(TAG, "onComplete File: " + file.getPath());
}
@Override
public void onFail(String msg) {
//下载失败
// Log.e(TAG, "onFail: " + msg);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
// dismiss();
}
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Service.BIND_AUTO_CREATE);
}
}