//初始化SD卡
private void initSD() {
? ? ? ? if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
? ? ? ? ? ? ? ? PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? readSD();
? ? ? ? } else {
? ? ? ? ? ? ActivityCompat.requestPermissions(this, new String[]{Manifest.permission
? ? ? ? ? ? ? ? ? ? .WRITE_EXTERNAL_STORAGE}, 100);
? ? ? ? }
? ? }
//權(quán)限處理
? ? @Override
? ? public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @NonNull int[] grantResults) {
? ? ? ? super.onRequestPermissionsResult(requestCode, permissions, grantResults);
? ? ? ? switch (requestCode) {
? ? ? ? ? ? case 100:
? ? ? ? ? ? ? ? if (grantResults.length > 0 && grantResults[0] == PackageManager
? ? ? ? ? ? ? ? ? ? ? ? .PERMISSION_GRANTED) {
? ? ? ? ? ? ? ? ? ? readSD();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
//讀取SD卡
? ? private void readSD() {
? ? ? ? if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
? ? ? ? ? ? sd = Environment.getExternalStorageDirectory();
? ? ? ? }
? ? }
private void moreHttp() {
? ? ? ? ThreadManager.getThreadManager().execute(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? //1.通過網(wǎng)絡(luò)加載獲取文件長度
? ? ? ? ? ? ? ? ? ? URL urlStr = new URL(url);
? ? ? ? ? ? ? ? ? ? HttpURLConnection con = (HttpURLConnection) urlStr.openConnection();
? ? ? ? ? ? ? ? ? ? int contentLength = con.getContentLength();
? ? ? ? ? ? ? ? ? ? Log.e(TAG, "run: " + contentLength);
? ? ? ? ? ? ? ? ? ? //2.c創(chuàng)建空白文件,指定長度
? ? ? ? ? ? ? ? ? ? RandomAccessFile rw = new RandomAccessFile(mDownPath, "rw");
? ? ? ? ? ? ? ? ? ? rw.setLength(contentLength);
? ? ? ? ? ? ? ? ? ? //3.各自線程設(shè)置下載對應(yīng)的長度
? ? ? ? ? ? ? ? ? ? long block = contentLength / THREAD_COUNT;
? ? ? ? ? ? ? ? ? ? //4.創(chuàng)建指定個數(shù)的線程并給定下載范圍下載
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < THREAD_COUNT; i++) {
? ? ? ? ? ? ? ? ? ? ? ? long start = i * block;
? ? ? ? ? ? ? ? ? ? ? ? long end = (i + 1) * block - 1;
? ? ? ? ? ? ? ? ? ? ? ? if (i == THREAD_COUNT - 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? end = contentLength - 1;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? //4.開始各自線程的下載即可(加請求范圍頭餐弱,判斷狀態(tài)碼206宴霸,從對應(yīng)線程開始位置寫入數(shù)據(jù))
? ? ? ? ? ? ? ? ? ? ? ? //down(i, start, end);//多線程下載
? ? ? ? ? ? ? ? ? ? ? ? downContinue(i, start, end);//多線程斷點下載
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } catch (MalformedURLException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
?? //斷點下載
private void downContinue(final int threadId, final long start, final long end) {
? ? ? ? Log.d(TAG, "線程: " + threadId + ",下載范圍:" + start + "--" + end);
? ? ? ? ThreadManager.getThreadManager().execute(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? //記錄當(dāng)前線程下載位置
? ? ? ? ? ? ? ? long currentPosition = start;
? ? ? ? ? ? ? ? //獲取sp中的下載位置,如果為0,說明沒有下載過或者下載完成過
? ? ? ? ? ? ? ? long position = (long)SharedPreferencesUtils.getParam(MainActivity.this, threadId + "",
? ? ? ? ? ? ? ? ? ? ? ? 0L);
? ? ? ? ? ? ? ? if (position == 0){//沒有下載記錄
? ? ? ? ? ? ? ? ? ? currentPosition = start;
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "非斷點續(xù)傳: "+threadId+",currentPosition:"+currentPosition);
? ? ? ? ? ? ? ? }else{//有級錄
? ? ? ? ? ? ? ? ? ? currentPosition = position;
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "斷點續(xù)傳: "+threadId+",currentPosition:"+currentPosition);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? URL urlStr = null;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? urlStr = new URL(url);
? ? ? ? ? ? ? ? ? ? HttpURLConnection con = (HttpURLConnection) urlStr.openConnection();
? ? ? ? ? ? ? ? ? ? //設(shè)置請求部分?jǐn)?shù)據(jù)資源:Range:bytes=10-30
? ? ? ? ? ? ? ? ? ? con.setRequestProperty("Range", "bytes=" + currentPosition + "-" + end);
? ? ? ? ? ? ? ? ? ? int responseCode = con.getResponseCode();
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "responseCode: " + responseCode);
? ? ? ? ? ? ? ? ? ? //206,請求部分資源
? ? ? ? ? ? ? ? ? ? if (responseCode == 206) {
? ? ? ? ? ? ? ? ? ? ? ? InputStream inputStream = con.getInputStream();
? ? ? ? ? ? ? ? ? ? ? ? RandomAccessFile rw = new RandomAccessFile(mDownPath, "rw");
? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置當(dāng)前線程寫入的位置
? ? ? ? ? ? ? ? ? ? ? ? rw.seek(currentPosition);
? ? ? ? ? ? ? ? ? ? ? ? int length = -1;
? ? ? ? ? ? ? ? ? ? ? ? byte[] bys = new byte[1024 * 10];
? ? ? ? ? ? ? ? ? ? ? ? while ((length = inputStream.read(bys)) != -1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? rw.write(bys, 0, length);
? ? ? ? ? ? ? ? ? ? ? ? ? ? currentPosition += length;
? ? ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "線程: " + threadId + "開始:" + start + " 當(dāng)前下載:" + currentPosition + " " +
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "結(jié)束:" + end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //將當(dāng)前寫入的位置保存
? ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferencesUtils.setParam(MainActivity.this,threadId+"",currentPosition);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (threadId == 0){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb.setProgress((int)currentPosition);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else? if (threadId == 1){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb1.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb1.setProgress((int)currentPosition);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else? if (threadId == 2){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb2.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb2.setProgress((int)currentPosition);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? ? ? ? ? ? ? rw.close();
? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "線程: " + threadId + "下載玩完畢");
? ? ? ? ? ? ? ? ? ? ? ? //完成下載后將當(dāng)前的寫入位置置零
? ? ? ? ? ? ? ? ? ? ? ? SharedPreferencesUtils.setParam(MainActivity.this,threadId+"",0L);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } catch (MalformedURLException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void down(final int threadId, final long start, final long end) {
? ? ? ? Log.d(TAG, "線程: " + threadId + ",下載范圍:" + start + "--" + end);
? ? ? ? ThreadManager.getThreadManager().execute(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? URL urlStr = null;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? urlStr = new URL(url);
? ? ? ? ? ? ? ? ? ? HttpURLConnection con = (HttpURLConnection) urlStr.openConnection();
? ? ? ? ? ? ? ? ? ? //設(shè)置請求部分?jǐn)?shù)據(jù)資源:Range:bytes=10-30
? ? ? ? ? ? ? ? ? ? con.setRequestProperty("Range", "bytes=" + start + "-" + end);
? ? ? ? ? ? ? ? ? ? int responseCode = con.getResponseCode();
? ? ? ? ? ? ? ? ? ? Log.d(TAG, "responseCode: " + responseCode);
? ? ? ? ? ? ? ? ? ? //206,請求部分資源
? ? ? ? ? ? ? ? ? ? if (responseCode == 206) {
? ? ? ? ? ? ? ? ? ? ? ? InputStream inputStream = con.getInputStream();
? ? ? ? ? ? ? ? ? ? ? ? RandomAccessFile rw = new RandomAccessFile(mDownPath, "rw");
? ? ? ? ? ? ? ? ? ? ? ? rw.seek(start);
? ? ? ? ? ? ? ? ? ? ? ? int count = (int) start;
? ? ? ? ? ? ? ? ? ? ? ? int length = -1;
? ? ? ? ? ? ? ? ? ? ? ? byte[] bys = new byte[1024 * 10];
? ? ? ? ? ? ? ? ? ? ? ? while ((length = inputStream.read(bys)) != -1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? rw.write(bys, 0, length);
? ? ? ? ? ? ? ? ? ? ? ? ? ? count += length;
? ? ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "線程: " + threadId + "開始:" + start + " 當(dāng)前下載:" + count + " " +
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "結(jié)束:" + end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (threadId == 0){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb.setProgress((int)count);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else? if (threadId == 1){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb1.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb1.setProgress((int)count);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else? if (threadId == 2){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb2.setMax((int)end);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pb2.setProgress((int)count);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? ? ? ? ? ? ? rw.close();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } catch (MalformedURLException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //Http下載
private void http(final String path) {
? ? ? ? ThreadManager.getThreadManager().execute(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? URL urlStr = new URL(url);
? ? ? ? ? ? ? ? ? ? HttpURLConnection con = (HttpURLConnection) urlStr.openConnection();
? ? ? ? ? ? ? ? ? ? int responseCode = con.getResponseCode();
? ? ? ? ? ? ? ? ? ? if (responseCode == 200) {
? ? ? ? ? ? ? ? ? ? ? ? InputStream inputStream = con.getInputStream();
? ? ? ? ? ? ? ? ? ? ? ? int max = con.getContentLength();
? ? ? ? ? ? ? ? ? ? ? ? saveFile(inputStream, path, max);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } catch (MalformedURLException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? //retrofit下載
? private void retrofit(final String path) {
? ? ? ? Retrofit retrofit = new Retrofit.Builder()
? ? ? ? ? ? ? ? .baseUrl(MyServer.Url)
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create())
? ? ? ? ? ? ? ? .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
? ? ? ? ? ? ? ? .build();
? ? ? ? MyServer myServer = retrofit.create(MyServer.class);
? ? ? ? Observable<ResponseBody> download = myServer.download();
? ? ? ? download.subscribeOn(Schedulers.io())
? ? ? ? ? ? ? ? .subscribe(new Observer<ResponseBody>() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onSubscribe(Disposable d) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onNext(ResponseBody responseBody) {
? ? ? ? ? ? ? ? ? ? ? ? long max = responseBody.contentLength();
? ? ? ? ? ? ? ? ? ? ? ? InputStream inputStream = responseBody.byteStream();
? ? ? ? ? ? ? ? ? ? ? ? saveFile(inputStream, path, max);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onError(Throwable e) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e(TAG, "onError: " + e.getMessage());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onComplete() {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }
//OkHttp下載
? ? private void ok(final String path) {
? ? ? ? OkHttpClient okHttpClient = new OkHttpClient.Builder()
? ? ? ? ? ? ? ? .build();
? ? ? ? Request request = new Request.Builder()
? ? ? ? ? ? ? ? .url(url)
? ? ? ? ? ? ? ? .get()
? ? ? ? ? ? ? ? .build();
? ? ? ? Call call = okHttpClient.newCall(request);
? ? ? ? call.enqueue(new Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? ? ? Log.e(TAG, "onFailure: " + e.getMessage());
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
? ? ? ? ? ? ? ? ResponseBody body = response.body();
? ? ? ? ? ? ? ? InputStream inputStream = body.byteStream();
? ? ? ? ? ? ? ? long max = body.contentLength();
? ? ? ? ? ? ? ? //文件下載保存
? ? ? ? ? ? ? ? saveFile(inputStream, path, max);
? ? ? ? ? ? }
? ? ? ? });
? ? }
//保存文件
? ? private void saveFile(InputStream inputStream, final String path, long max) {
? ? ? ? try {
? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(new File(path));
? ? ? ? ? ? long count = 0;
? ? ? ? ? ? int length = -1;
? ? ? ? ? ? byte[] bys = new byte[1024 * 10];
? ? ? ? ? ? while ((length = inputStream.read(bys)) != -1) {
? ? ? ? ? ? ? ? fos.write(bys, 0, length);
? ? ? ? ? ? ? ? count += length;
? ? ? ? ? ? ? ? Log.e(TAG, "count: " + count + ", max:" + max);
? ? ? ? ? ? ? ? //進度條和視頻播放SurfaceView可以直接在子線程中刷新
? ? ? ? ? ? ? ? pb.setMax((int) max);
? ? ? ? ? ? ? ? pb.setProgress((int) count);
? ? ? ? ? ? }
? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? fos.close();
? ? ? ? ? ? runOnUiThread(new Runnable() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "下載完畢", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? //apk安裝處理
? ? ? ? ? ? ? ? ? ? mPath = path;
? ? ? ? ? ? ? ? ? ? InstallUtil.installApk(MainActivity.this, path);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /**
? ? * 8.0安裝處理???????? 下載完自動安裝
? ? */
? ? private String mPath = null;
? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? if (resultCode == RESULT_OK && requestCode == InstallUtil.UNKNOWN_CODE) {
? ? ? ? ? ? InstallUtil.installApk(MainActivity.this, mPath);//再次執(zhí)行安裝流程,包含權(quán)限判等
? ? ? ? }
? ? }
}
//自動安裝工具類
public class InstallUtil {
? ? public static final int UNKNOWN_CODE = 2019;
? ? public static void installApk(Context context, String path) {
? ? ? ? if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
? ? ? ? ? ? startInstallO(context,path);
? ? ? ? }else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
? ? ? ? ? ? startInstallN(context,path);
? ? ? ? }else {
? ? ? ? ? ? startInstall(context,path);
? ? ? ? }
? ? }
? ? /**
? ? *android1.x-6.x
? ? *@param path 文件的路徑
? ? */
? ? private static void startInstall(Context context, String path) {
? ? ? ? Intent install = new Intent(Intent.ACTION_VIEW);
? ? ? ? install.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
? ? ? ? install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? context.startActivity(install);
? ? }
? ? /**
? ? * android7.x
? ? * @param path 文件路徑
? ? *
? ? * 清單文件配置
? ? *? ? ? ? ? <provider
? ? *? ? ? ? ? ? android:authorities="com.baidu.download.provider"
? ? *? ? ? ? ? ? android:name="android.support.v4.content.FileProvider"
? ? *? ? ? ? ? ? android:exported="false"
? ? *? ? ? ? ? ? android:grantUriPermissions="true">
? ? *? ? ? ? ? ? <meta-data
? ? *? ? ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"
? ? *? ? ? ? ? ? ? ? android:resource="@xml/file_paths"/>
? ? *? ? ? ? </provider>
? ? *
? ? *
? ? *? ? ? ? ? <files-path/>代表的根目錄: Context.getFilesDir()
? ? *? ? ? ? ? <external-path/>代表的根目錄: Environment.getExternalStorageDirectory()
? ? *? ? ? ? ? <cache-path/>代表的根目錄: getCacheDir()
? ? */
? ? @RequiresApi(api = Build.VERSION_CODES.N)
? ? private static void startInstallN(Context context, String path) {
? ? ? ? //參數(shù)1 上下文, 參數(shù)2 在AndroidManifest中的android:authorities值, 參數(shù)3? 共享的文件
? ? ? ? Uri apkUri = FileProvider.getUriForFile(context, "com.baidu.download.provider", new File(path));
? ? ? ? Intent install = new Intent(Intent.ACTION_VIEW);
? ? ? ? //由于沒有在Activity環(huán)境下啟動Activity,設(shè)置下面的標(biāo)簽
? ? ? ? install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? //添加這一句表示對目標(biāo)應(yīng)用臨時授權(quán)該Uri所代表的文件
? ? ? ? install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
? ? ? ? install.setDataAndType(apkUri, "application/vnd.android.package-archive");
? ? ? ? context.startActivity(install);
? ? }
? ? /**
? ? * android8.x
? ? *
? ? *? ? ? ? <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
? ? *
? ? *? ? ? ? 調(diào)起未知應(yīng)用來源
? ? *? ? ? ? 回調(diào)處理:
? ? *
? ? ? ? ? ? *? private String mPath = null;
? ? ? ? ? ? *? ? @Override
? ? ? ? ? ? *? ? protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
? ? ? ? ? ? *? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? ? ? *? ? ? ? if (resultCode == RESULT_OK && requestCode == InstallUtil.UNKNOWN_CODE) {
? ? ? ? ? ? *? ? ? ? ? ? InstallUtil.installApk(MainActivity.this,mPath);//再次執(zhí)行安裝流程膏蚓,包含權(quán)限判等
? ? ? ? ? ? *? ? ? ? }
? ? ? ? ? ? *? ? }
? ? *
? ? */
? ? @RequiresApi(api = Build.VERSION_CODES.O)
? ? private static void startInstallO(final Context context, String path) {
? ? ? ? boolean isGranted = context.getPackageManager().canRequestPackageInstalls();
? ? ? ? if (isGranted) startInstallN(context,path);//安裝應(yīng)用的邏輯(寫自己的就可以)
? ? ? ? else new AlertDialog.Builder(context)
? ? ? ? ? ? ? ? .setCancelable(false)
? ? ? ? ? ? ? ? .setTitle("安裝應(yīng)用需要打開未知來源權(quán)限瓢谢,請去設(shè)置中開啟權(quán)限")
? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface d, int w) {
? ? ? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
? ? ? ? ? ? ? ? ? ? ? ? Activity act = (Activity) context;
? ? ? ? ? ? ? ? ? ? ? ? act.startActivityForResult(intent, UNKNOWN_CODE);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .show();
? ? }
}