OkHttp、Retrofit类缤、Http臼勉、斷點下載

//初始化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();

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市降允,隨后出現(xiàn)的幾起案子恩闻,更是在濱河造成了極大的恐慌,老刑警劉巖剧董,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件幢尚,死亡現(xiàn)場離奇詭異,居然都是意外死亡翅楼,警方通過查閱死者的電腦和手機尉剩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來毅臊,“玉大人理茎,你說我怎么就攤上這事」苕遥” “怎么了皂林?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蚯撩。 經(jīng)常有香客問我础倍,道長,這世上最難降的妖魔是什么胎挎? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任沟启,我火速辦了婚禮,結(jié)果婚禮上犹菇,老公的妹妹穿的比我還像新娘德迹。我一直安慰自己,他們只是感情好揭芍,可當(dāng)我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布胳搞。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪流酬。 梳的紋絲不亂的頭發(fā)上币厕,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天,我揣著相機與錄音芽腾,去河邊找鬼。 笑死页衙,一個胖子當(dāng)著我的面吹牛摊滔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播店乐,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼艰躺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了眨八?” 一聲冷哼從身側(cè)響起腺兴,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎廉侧,沒想到半個月后页响,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡段誊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年闰蚕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片连舍。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡没陡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出索赏,到底是詐尸還是另有隱情盼玄,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布潜腻,位于F島的核電站埃儿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏砾赔。R本人自食惡果不足惜蝌箍,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望暴心。 院中可真熱鬧妓盲,春花似錦、人聲如沸专普。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至筋粗,卻和暖如春策橘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背娜亿。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工丽已, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人买决。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓沛婴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親督赤。 傳聞我的和親對象是個殘疾皇子嘁灯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容