MD5 校验值:7cd346351ab05a28e42aff65173e0141
ApkInstaller.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.leptonsoftware.fibernet.downloadapk; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.util.Log; import androidx.core.content.FileProvider; import com.leptonsoftware.fibernet.toasty.Toasty; import io.fabric.sdk.android.services.common.a; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.Objects; public class ApkInstaller extends AsyncTask<String, Integer, String> { private Context mContext; private ProgressDialog mPDialog; private void installApk() { try { File externalFilesDir = this.mContext.getExternalFilesDir(null); Objects.requireNonNull(externalFilesDir); File file = new File(externalFilesDir.getAbsolutePath() + "/my_apk.apk"); Intent intent = new Intent("android.intent.action.VIEW"); if (Build.VERSION.SDK_INT >= 24) { Uri e10 = FileProvider.e(this.mContext, "com.jfp.prod.provider", file); intent.setDataAndType(e10, "application/vnd.android.package-archive"); for (ResolveInfo resolveInfo : this.mContext.getPackageManager().queryIntentActivities(intent, 65536)) { this.mContext.grantUriPermission(this.mContext.getApplicationContext().getPackageName() + ".provider", e10, 3); } intent.setFlags(335544323); this.mContext.startActivity(intent); } else { intent.setAction("android.intent.action.VIEW"); intent.addFlags(1); intent.putExtra("android.intent.extra.NOT_UNKNOWN_SOURCE", true); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); intent.setFlags(268435456); } this.mContext.startActivity(intent); } catch (Exception e11) { e11.printStackTrace(); } } @Override protected void onPreExecute() { super.onPreExecute(); ProgressDialog progressDialog = this.mPDialog; if (progressDialog != null) { progressDialog.show(); } } public void setContext(Activity activity) { this.mContext = activity; activity.runOnUiThread(new Runnable() { @Override public void run() { ApkInstaller.this.mPDialog = new ProgressDialog(ApkInstaller.this.mContext); ApkInstaller.this.mPDialog.setMessage("Please wait..."); ApkInstaller.this.mPDialog.setIndeterminate(true); ApkInstaller.this.mPDialog.setProgressStyle(1); ApkInstaller.this.mPDialog.setCancelable(false); ApkInstaller.this.mPDialog.show(); } }); } @Override public String doInBackground(String... strArr) { try { URLConnection openConnection = new URL(strArr[0]).openConnection(); openConnection.setDoInput(true); openConnection.setDoOutput(false); openConnection.setUseCaches(false); openConnection.setRequestProperty(a.HEADER_USER_AGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); openConnection.connect(); int contentLength = openConnection.getContentLength(); File externalFilesDir = this.mContext.getExternalFilesDir(null); Objects.requireNonNull(externalFilesDir); File file = new File(externalFilesDir.getAbsolutePath()); file.mkdirs(); File file2 = new File(file, "my_apk.apk"); if (file2.exists()) { file2.delete(); } FileOutputStream fileOutputStream = new FileOutputStream(file2); InputStream inputStream = openConnection.getInputStream(); byte[] bArr = new byte[1024]; long j10 = 0; while (true) { int read = inputStream.read(bArr); if (read == -1) { break; } j10 += read; fileOutputStream.write(bArr, 0, read); publishProgress(Integer.valueOf((int) ((100 * j10) / contentLength))); } fileOutputStream.close(); inputStream.close(); ProgressDialog progressDialog = this.mPDialog; if (progressDialog != null) { progressDialog.dismiss(); } installApk(); } catch (Exception e10) { Log.e("UpdateAPP", "Update error! " + e10.getMessage()); } return null; } @Override public void onPostExecute(String str) { ProgressDialog progressDialog = this.mPDialog; if (progressDialog != null) { progressDialog.dismiss(); } if (str == null) { Toasty.info(this.mContext, "Download error: " + str); return; } Toasty.info(this.mContext, "File Downloaded:" + str); } @Override public void onProgressUpdate(Integer... numArr) { super.onProgressUpdate((Object[]) numArr); ProgressDialog progressDialog = this.mPDialog; if (progressDialog != null) { progressDialog.setIndeterminate(false); this.mPDialog.setMax(100); this.mPDialog.setProgress(numArr[0].intValue()); } } }