有沒有案例或者框架之類的,麻煩了,謝謝
uj5u.com熱心網友回復:
你有一個apk的下載地址,然后就隨便找一個下載檔案的代碼就行了,volley,okhttp,retrofit等等都可以uj5u.com熱心網友回復:
基本上網路庫都有下載資源的類 ,至于下載app檔案下載到哪,下載時的樣式,下載完后的安裝,各版本的安裝適配, 都是你自己代碼實作的.uj5u.com熱心網友回復:
public class DownloadApk {
private volatile static DownloadApk mInstance = null;
private Context mContext;
private DownloadApk() {
}
public static DownloadApk getInstance() {
if (mInstance == null) {
synchronized (DownloadApk.class) {
if (mInstance == null) {
mInstance = new DownloadApk();
}
}
}
return mInstance;
}
public void downLoadApk(final Context mContext, final String url) {
this.mContext = mContext;
//進度條
mProgressDialog = new Dialog(mContext, R.style.FullHeightDialog);
mProgressDialog.setContentView(R.layout.loading_dialog);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgress = mProgressDialog.findViewById(R.id.progressBar);
mTvProgressCount = mProgressDialog.findViewById(R.id.tv_progress_count);
ImageView ivDismiss = mProgressDialog.findViewById(R.id.iv_dismiss);
ivDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(mContext, "靜默下載中...", Toast.LENGTH_SHORT).show();
progressDismiss();
}
});
mProgress.setMax(100);
mProgressDialog.show();
new Thread() {
@Override
public void run() {
try {
URL urls = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setConnectTimeout(5000);
conn.setUseCaches(false);//不使用快取
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//獲取到檔案的大小
int contentLength = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory() + "/book.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
if (System.currentTimeMillis() - curTime > 1000 || total == contentLength) {
//獲取當前下載量
long progress = total * 100L / contentLength;
//UI主執行緒中,直接設定會報例外
mProgress.setProgress((int) progress);
Message message = new Message();
message.what = 0;
message.arg1 = (int) progress;
handler.sendMessage(message);
curTime = System.currentTimeMillis();
}
}
fos.close();
bis.close();
is.close();
//安裝APK
installApk(file, mContext);
progressDismiss(); //結束掉進度條對話框
}
else {
//ToastUtil(mContext,"應用安裝包不存在");
Message message = new Message();
message.what = 1;
Bundle bundle = new Bundle();
bundle.putString("Error","應用安裝包不存在");
message.setData(bundle);
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private void progressDismiss() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
private Dialog mProgressDialog;
private ProgressBar mProgress;
private TextView mTvProgressCount;
private long curTime = 0;
public void installApk(File file, Context mContext) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Android7.0及以上版本
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//(mContext, "應用包名" + ".fileProvider", file)
Uri contentUri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName() + ".fileprovider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
// Android7.0以下版本
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
//progressDismiss();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
int progress = msg.arg1;
mTvProgressCount.setText(progress + "%");
break;
case 1:
String rs = msg.getData().getString("Error");
AlertDialog dialogs = new AlertDialog.Builder(mContext)
.setTitle("訪問錯誤")
.setMessage(rs + "或服務器無回應\n\n請聯系管理員")
.setCancelable(true).show();
break;
default:
break;
}
}
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/57824.html
標籤:Android
上一篇:軟體開發與編程的區別
