{"id":6558,"date":"2024-08-04T13:01:03","date_gmt":"2024-08-04T05:01:03","guid":{"rendered":""},"modified":"2024-08-04T13:01:03","modified_gmt":"2024-08-04T05:01:03","slug":"android\u5e94\u7528\u66f4\u65b0_appstore\u6ca1\u6709\u66f4\u65b0\u9009\u9879","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6558.html","title":{"rendered":"android\u5e94\u7528\u66f4\u65b0_appstore\u6ca1\u6709\u66f4\u65b0\u9009\u9879"},"content":{"rendered":"
\u57fa\u672c\u4e0a\u4ec0\u4e48App\u90fd\u4f1a\u6709\u7684\u529f\u80fd\uff0c\u5728\u6b64\u5217\u4e3e\u4e00\u4e2a\u4e4b\u524d\u9879\u76ee\u4f7f\u7528\u7684\uff0c\u8bf7\u6c42\u662fRxjava+Retrofit\uff0c\u663e\u793a\u8fdb\u5ea6\u662f\u901a\u77e5\u680f\u8fdb\u5ea6\u6761\u3002<\/p>\n
<\/p>\n
1.\u5224\u65ad\u662f\u5426\u9700\u8981\u66f4\u65b0\u90e8\u5206<\/p>\n
2.\u8bf7\u6c42\u4e0b\u8f7dapk<\/p>\n
3.\u8fdb\u5ea6\u6761\u66f4\u65b0<\/p>\n
4.\u4e0b\u8f7d\u540e\u81ea\u52a8\u5b89\u88c5<\/p>\n
5.\u5176\u4ed6\u95ee\u9898<\/p>\n
\u6d4b\u8bd5demo\u8bf7\u53c2\u8003QingFrame\u4e2d\u7684\u76f8\u5173\u6d4b\u8bd5\u6a21\u5757\uff1a<\/p>\n
https:\/\/github.com\/UncleQing\/QingFrame<\/p>\n
1.\u5224\u65ad\u662f\u5426\u9700\u8981\u66f4\u65b0\u90e8\u5206<\/strong><\/p>\n \u8fd9\u90e8\u5206\u5f88\u7b80\u5355\uff0c\u57fa\u672c\u5c31\u662f\u8bf7\u6c42\u81ea\u5df1\u670d\u52a1\u5668\u76f8\u5173\u63a5\u53e3\uff0c\u670d\u52a1\u5668\u6839\u636e\u6211\u4eec\u4e0a\u4f20\u7684\u7248\u672c\u53f7\u8fd4\u56de\u4fe1\u606f\uff0c\u5982\u679c\u6ca1\u6709\u8fd4\u56de\u4fe1\u606f\u5219\u4e0d\u9700\u66f4\u65b0\uff0c\u5982\u679c\u6709\u518d\u5224\u5b9a\u662f\u5426\u9700\u8981\u5f3a\u5236\u66f4\u65b0\uff08\u8fdb\u5165\u9996\u9875\u81ea\u52a8\u83b7\u53d6\uff09\uff0c\u53ea\u8981\u9700\u8981\u66f4\u65b0\u5219\u663e\u793a\u4e00\u4e2adialog\uff0c\u663e\u793a\u51fa\u66f4\u65b0\u7ec6\u8282\u4ee5\u53ca\u4e00\u4e2aButton\uff0c\u70b9\u51fb\u8fdb\u884c\u4e0b\u8f7dapk<\/p>\n \u4e0b\u9762\u662f\u8bf7\u6c42\u540e\u7684\u56de\u8c03\uff0c\u5982\u679cbeans\u4e0d\u4e3a\u7a7a\u5219\u9700\u8981\u66f4\u65b0\uff0c\u6839\u636ebeans\u4e2d\u5e26\u6709url\u8bf7\u6c42\u4e0b\u8f7d<\/p>\n <\/p>\n 2.\u8bf7\u6c42\u4e0b\u8f7dapk<\/strong><\/p>\n \u8fd9\u90e8\u5206\u6839\u636e\u4f7f\u7528\u7684\u7f51\u7edc\u6846\u67b6\u4e0d\u540c\u5b9e\u73b0\u4e0d\u540c\uff0c\u53ea\u4ecb\u7ecdRxjava+Retrofit\u8fd9\u79cd\u7684<\/p>\n Presenter\uff1a<\/p>\n Retrofit\u8c03\u7528\u63a5\u53e3<\/p>\n \u8bf7\u6c42\u65b9\u5f0fGet\u4ee5\u6d41\u5f62\u5f0f\u83b7\u53d6\uff0c\u83b7\u53d6\u6210\u529f\u540e\u5728presenter\u7684onNext\u65b9\u6cd5\u5199\u5165\u5230\u76f8\u5173\u6587\u4ef6<\/p>\n 3.\u8fdb\u5ea6\u6761\u66f4\u65b0<\/strong><\/p>\n \u4e0a\u4e00\u6b65\u5b8c\u6210\u540e\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230apk\u6587\u4ef6\u4e86\uff0c\u4f46\u662f\u4e0b\u8f7d\u8fc7\u7a0bUI\u6ca1\u6709\u4efb\u4f55\u663e\u793a\uff0c\u56e0\u6b64\u9700\u8981\u5728\u4e0b\u8f7d\u8fc7\u7a0b\u4e2d\u663e\u793a\u5728\u901a\u77e5\u680f\u4e0a\u3002<\/p>\n \u9996\u5148\uff0c\u914d\u7f6eOKHttp\u65f6\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4e0b\u8f7d\u62e6\u622a\u5668<\/p>\n <\/p>\n \u7136\u540e\u5728\u7b2c\u4e8c\u6b65Retrofit\u63a5\u53e3\u8bf7\u6c42\u8981\u5e26\u5165\u8fd9\u4e2a\u5934\u4fe1\u606f\uff0c\u8fd9\u6837\u4e0b\u8f7d\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u51fa\u53d1\u6211\u4eec\u81ea\u5b9a\u4e49\u7684body\u4e86<\/p>\n ProgressResponseBody<\/p>\n \u53d1\u51fa\u4e86\u5e7f\u64ad\uff0c\u5728\u76f8\u5173activity\u6ce8\u518c\u5e7f\u64ad\u63a5\u6536\u5373\u53ef<\/p>\n \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u57288.0Notification\u884c\u4e3a\u53d8\u66f4\uff0c\u9700\u8981\u6709\u6e20\u9053\u76f8\u5173\u4fe1\u606f\uff0c\u53e6\u5916\u5982\u679cnotification\u7684\u5c0f\u56fe\u6807\u662f\u9700\u8981\u4e00\u4e2a\u6ca1\u6709\u989c\u8272\u5207\u56fe\u3002<\/p>\n 4.\u4e0b\u8f7d\u540e\u81ea\u52a8\u5b89\u88c5<\/strong><\/p>\n \u4e0a\u9762\u8bbe\u7f6e\u5b8c\u540e\u5c31\u53ef\u4ee5\u5b8c\u6574\u4e0b\u8f7d\u4e00\u4e2aapk\u5230\u672c\u5730\u4e86\uff0c\u63a5\u4e0b\u6765\u662f\u81ea\u52a8\u5b89\u88c5\u3002<\/p>\n \u8bbe\u7f6e\u5b8c\u6210\u53bb\u5e94\u7528\u5e02\u573a\u627e\u4e2aapk\u94fe\u63a5\u8bd5\u4e0b\u5427\uff0c\u4e3a\u4e86\u6392\u9664\u975e\u4ee3\u7801\u539f\u56e0\u7684\u5b89\u5353\u5931\u8d25\uff0c\u5efa\u8bae\u94fe\u63a5\u5148\u4ece\u6d4f\u89c8\u5668\u4e0b\u8f7d\u540e\u624b\u52a8\u5b89\u88c5\uff0c\u786e\u8ba4\u65e0\u8bef\u518d\u4f7f\u7528\u4ee3\u7801\u6d4b\u8bd5<\/p>\n 5.\u5176\u4ed6\u95ee\u9898<\/strong><\/p>\n \u518d\u6b21\u91cd\u7533\u51e0\u4e2a\u95ee\u9898<\/p>\n 1.\u4e0b\u8f7d\u5b89\u88c5apk\u9700\u8981\u7684\u6743\u9650\uff0csd\u5361\u8bfb\u53d6\u548c\u7f51\u7edc\u8bf7\u6c42\u5c31\u4e0d\u8bf4\u4e86\uff0c\u8fd8\u9700\u8981\u4e00\u4e2a<\/p>\n 2.\u89e3\u6790apk\u7684contentUri\u9700\u8981\u914d\u7f6efileprovider\uff0c\u4e0d\u8981\u5199\u9519\u4e86<\/p>\n \u53c2\u8003\uff1aAndroid App\u7684\u66f4\u65b0<\/p>\n 3.Retrofit\u63a5\u53e3\u5982\u679c\u6309\u6211\u7684\u8fd9\u4e2a\u8bb0\u5f97\u4f20\u4e2a\u5934\u4fe1\u606f\"upgrade\"\uff0c\u53e6\u5916\u5982\u679cOKhttp\u4e5f\u662f\u914d\u7f6e\u7f13\u5b58\u7684\u8bdd\u9700\u8981\u53bb\u6389\u7f13\u5b58\u8bf7\u6c42<\/p>\n 4.Notification\u4e2d\u5c0f\u56fe\u6807\u5982\u679c\u662f\u6709\u989c\u8272\u7684\u4f1a\u663e\u793a\u767d\u8272\u65b9\u5757\uff0c\u6240\u4ee5\u9700\u8981\u7f8e\u5de5\u5355\u72ec\u5207\u4e2a\u6ca1\u6709\u989c\u8272\u7684\u56fe\uff0c\u7136\u540e\u6211\u4eec\u81ea\u5df1\u914d\u7f6e\u4e0a\u989c\u8272<\/p>\n \u4ee5\u4e0a\u8d34\u7684\u4ee3\u7801\u5728\u6587\u9996\u94fe\u63a5\u90a3\u4e2ademo\u90fd\u6709\uff0c\u53ef\u80fd\u6709\u4e9b\u53d9\u8ff0\u7684\u4e0d\u5f7b\u5e95\uff0c\u60f3\u53c2\u8003\u7684\u670b\u53cb\u8bf7\u76f4\u63a5\u53bbdemo\u4e2d\u770b\u5427\u3002\u53e6\u5916\u5173\u4e8e\u5f3a\u5236\u66f4\u65b0\u518d\u8bf4\u4e00\u4e0b\uff0c\u4e00\u822c\u7684\u90fd\u662f\u8fdb\u5165\u9996\u9875\u83b7\u53d6\u66f4\u65b0\u4fe1\u606f\uff0c\u5982\u679c\u9700\u8981\u5f3a\u5236\u66f4\u65b0\uff0c\u5219\u5f39\u51fa\u7684\u8fd9\u4e2a\u66f4\u65b0\u5bf9\u8bdd\u6846\u4e0d\u53ef\u5173\u95ed\uff0c\u5373\u7528\u6237\u5fc5\u987b\u70b9\u51fb\u201c\u7acb\u5373\u66f4\u65b0\u201d\u7684\u6309\u94ae\uff0c\u5426\u5219\u4e0d\u53ef\u7528\uff0c\u70b9\u51fb\u4e4b\u540e\u5206\u4e24\u79cd\uff0c\u4e00\u79cd\u53ef\u4ee5\u8ba9\u7528\u6237\u7ee7\u7eed\u4f7f\u7528\uff0c\u76f4\u5230\u4e0b\u8f7d\u5b8c\u6210\u540e\u8df3\u8f6c\u5230\u5b89\u88c5\u754c\u9762\uff1b\u53e6\u4e00\u79cd\u662f\u5c06\u66f4\u65b0\u8fdb\u5ea6\u4e5f\u662fdialog\u663e\u793a\uff0c\u5e76\u4e14\u4e5f\u662f\u4e0d\u53ef\u5173\u95ed\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"android\u5e94\u7528\u66f4\u65b0_appstore\u6ca1\u6709\u66f4\u65b0\u9009\u9879\u524d\u8a00\u57fa\u672c\u4e0a\u4ec0\u4e48App\u90fd\u4f1a\u6709\u7684\u529f\u80fd\uff0c\u5728\u6b64\u5217\u4e3e\u4e00\u4e2a\u4e4b\u524d\u9879\u76ee\u4f7f\u7528\u7684\uff0c\u8bf7\u6c42\u662fRxjava+Retrofit...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6558"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6558"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6558\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}public void onLoadingVersionSucceed(final List<VersionBean> beans) { if (beans == null || beans.size() == 0) { MyToast.showToast(getContext(), \"\u5f53\u524d\u5df2\u662f\u6700\u65b0\u7248\u672c\"); } else { \/\/\u66f4\u65b0\u8be2\u95ee\u5bf9\u8bdd\u6846 DialogFragmentHelper.showUpdateDialog(getFragmentManager(), beans.get(0).getCopywriting() , true, new View.OnClickListener() { @Override public void onClick(View view) { \/\/\u4e0b\u8f7dapk mPresenter.downLoadApk(beans.get(0).getDownload_url()); } }); } } <\/code><\/pre>\n
\"copywriting\": \"\u66f4\u65b0\u5185\u5bb9:...\", \"forced_update\": \"\u662f\u5426\u5f3a\u5236\u66f4\u65b0 1 \u662f,2 \u5426\", \"download_url\": \"\u4e0b\u8f7d\u5730\u5740\" <\/code><\/pre>\n
public class UpdatePresenter { \/\/\u8def\u5f84\uff1adata\/\u5305\u540d\/file\/\uff0c\u4fdd\u8bc1app\u5378\u8f7d\u540e\u4e0d\u4f1a\u6b8b\u7559\u5783\u573e private final static String DOWNLOAD_DIR = AppUtils.getApp().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + File.separator; \/\/\u4e0b\u8f7d\u540e\u7684apk\u6587\u4ef6\u540d\u5b57\uff0c\u4fdd\u8bc1\u540e\u7f00.apk\u5c31\u884c private static String APP_FILE = \"qingFrame.apk\"; \/\/\u76f8\u5173\u7684activity private UpdateActivity mView; UpdatePresenter(IBaseView view) { mView = (UpdateActivity) view; } \/** * \u6839\u636eurl\u4e0b\u8f7dapk * * @param url *\/ public void downLoadApk(String url) { if (AppConfig.isDownLoad) { mView.showToast(\"\u5df2\u6709\u76f8\u540c\u4efb\u52a1\"); return; } RetrofitApiService.getInstance().downloadApk(url).subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { mView.showToast(\"\u5df2\u6dfb\u52a0\u5230\u4e0b\u8f7d\u4efb\u52a1\"); AppConfig.isDownLoad = true; } @Override public void onNext(ResponseBody responseBody) { File file = FileUtils.get().writeFile(responseBody.byteStream(), DOWNLOAD_DIR, APP_FILE); mView.onDownloadSucceed(file); AppConfig.isDownLoad = false; } @Override public void onError(Throwable e) { if (!TextUtils.isEmpty(e.getMessage())) { mView.showToast(e.getMessage()); } AppConfig.isDownLoad = false; } @Override public void onComplete() { AppConfig.isDownLoad = false; } }); } }<\/code><\/pre>\n
RetrofitApiService<\/code><\/pre>\n
\/** * \u4e0b\u8f7dapk * * @return *\/ public Observable<ResponseBody> downloadApk(String url) { \/\/\u4f20\u5165\u5934\u4fe1\u606f\"upgrade\"\u662f\u4e3a\u4e86\u89e6\u53d1\u81ea\u5b9a\u4e49\u7684\u4e0b\u8f7d\u62e6\u622a\u5668 return mApiService.download(url, \"upgrade\"); }<\/code><\/pre>\n
IApiService<\/code><\/pre>\n
\/** * \u4e0b\u8f7dapk\uff0c \u4e0d\u4f7f\u7528\u7f13\u5b58,\u56e0\u4e3a\u914d\u7f6eOKhttp\u914d\u7f6e\u7f13\u5b58\u4e86\uff0c\u5982\u679c\u6b64\u90e8\u5206\u4f9d\u7136\u4f7f\u7528\u7f13\u5b58\u5219\u53ea\u4f1a\u4e0b\u8f7d\u4e00\u6b21 * @param url * @param head * @return *\/ @Streaming @GET @Headers({\"Accept-Encoding:identity\",\"Cache-Control:no-store\"}) Observable<ResponseBody> download(@Url String url, @Header(\"Upgrade\") String head);<\/code><\/pre>\n
writeFile<\/code><\/pre>\n
\/** * \u5c06\u8f93\u5165\u6d41\u5199\u5165\u6587\u4ef6 * * @param inputString * @param fileDir * @param fileName *\/ public File writeFile(InputStream inputString, String fileDir, String fileName) { File dir = new File(fileDir); if (!dir.exists()) { dir.mkdirs(); } File file = new File(fileDir, fileName); if (file.exists()) { file.delete(); } FileOutputStream fos = null; try { fos = new FileOutputStream(file); byte[] b = new byte[1024]; int len; while ((len = inputString.read(b)) != -1) { fos.write(b, 0, len); } inputString.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); LogUtils.e(\"Exception: \" + e.getMessage()); } return file; }<\/code><\/pre>\n
\/** * \u4e0b\u8f7d\u62e6\u622a\u5668\uff0c\u7528\u4e8e\u8fdb\u5ea6\u6761\u663e\u793a *\/ public class DownloadInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); String value = request.header(\"Upgrade\"); if (!TextUtils.isEmpty(value) || \"upgrade\".equals(value)) { \/\/\u53ea\u6709\u662f\u4e0b\u8f7d\u7684\u65f6\u5019\u4f1a\u5305\u542b\u8fd9\u4e2aUpgrade\u5934\u4fe1\u606f return response.newBuilder() .body(new ProgressResponseBody(response.body())) .build(); } return response; } }<\/code><\/pre>\n
public class ProgressResponseBody extends ResponseBody { private final ResponseBody responseBody; private BufferedSource bufferedSource; public ProgressResponseBody(ResponseBody responseBody) { this.responseBody = responseBody; } @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() { return responseBody.contentLength(); } @Override public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); } return bufferedSource; } private Source source(Source source) { return new ForwardingSource(source) { long totalBytesRead = 0L; @Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead += bytesRead != -1 ? bytesRead : 0; Intent mIntent = new Intent(AppConfig.DOWNLOAD_PROGRESS); mIntent.putExtra(\"totalBytesRead\", totalBytesRead); mIntent.putExtra(\"total\", responseBody.contentLength()); \/\/\u5c06\u4e0b\u8f7d\u8fdb\u5ea6\u901a\u8fc7\u5e7f\u64ad\u53d1\u9001\u51fa\u53bb AppUtils.getApp().sendBroadcast(mIntent); return bytesRead; } }; } }<\/code><\/pre>\n
UpdateActivity<\/code><\/pre>\n
private void registerReceiver() { \/\/\u4e0b\u8f7d\u8fdb\u5ea6\u6761 if (null == mReceiver) { mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (AppConfig.DOWNLOAD_PROGRESS.equals(action)) { double total = (double) intent.getLongExtra(\"total\", 0); double current = (double) intent.getLongExtra(\"totalBytesRead\", 0); if (!isCreat) { builder = UIUtils.creatNotification(notificationManager); isCreat = true; pro = 0; } pro = (int) (current \/ total * 100); if (pro < 100) { builder.setContentText(pro + \"%\"); builder.setProgress(100, pro, false); notificationManager.notify(UIUtils.notifyID, builder.build()); } else { UIUtils.cancleNotification(notificationManager); isCreat = false; } } } }; IntentFilter filter = new IntentFilter(); filter.addAction(AppConfig.DOWNLOAD_PROGRESS); AppUtils.getApp().registerReceiver(mReceiver, filter); } }<\/code><\/pre>\n
UIUtils<\/code><\/pre>\n
\/** * \u4e0b\u8f7dapk\u901a\u77e5\u680f * @param notificationManager * @return *\/ public static final int notifyID = 99; public static final String CHANNEL_ID = \"111\"; public static Notification.Builder creatNotification(NotificationManager notificationManager) { if (notificationManager == null) { notificationManager = (NotificationManager) AppUtils.getApp().getSystemService(Context.NOTIFICATION_SERVICE); } Notification.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { \/\/8.0\u65b0\u589e\u901a\u77e5\u6e20\u9053 CharSequence name = \"QingFrame\"; int importance = NotificationManager.IMPORTANCE_LOW; \/\/\u4f18\u5148\u7ea7 NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); mChannel.enableLights(false); \/\/\u95ea\u706f\u5f00\u5173 mChannel.enableVibration(false); \/\/\u632f\u52a8\u5f00\u5173 mChannel.setShowBadge(false); \/\/\u901a\u77e5\u5706\u70b9\u5f00\u5173 notificationManager.createNotificationChannel(mChannel); builder = new Notification.Builder(AppUtils.getApp(), CHANNEL_ID); } else { builder = new Notification.Builder(AppUtils.getApp()); } builder.setSmallIcon(R.drawable.ic_app_ntfc) .setLargeIcon(BitmapFactory.decodeResource(AppUtils.getApp().getResources(), R.mipmap.share_)) .setContentText(\"0%\") .setContentTitle(\"\u9752\u7ed3\u6784\") .setProgress(100, 0, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { \/\/\u5c0f\u56fe\u6807\u80cc\u666f builder.setColor(AppUtils.getApp().getResources().getColor(R.color.colorPrimary)); } notificationManager.notify(notifyID, builder.build()); return builder; } public static void cancleNotification(NotificationManager notificationManager) { if (notificationManager == null) { notificationManager = (NotificationManager) AppUtils.getApp().getSystemService(Context.NOTIFICATION_SERVICE); } notificationManager.cancel(notifyID); }<\/code><\/pre>\n
CommonUtil<\/code><\/pre>\n
\/\/\u81ea\u52a8\u5b89\u88c5apk public static void installApk(Context context, File apkPath) { \/\/\u63d0\u5347\u6587\u4ef6\u8bfb\u5199\u6743\u9650 String command = \"chmod \" + \"777\" + \" \" + apkPath.getPath(); Runtime runtime = Runtime.getRuntime(); try { runtime.exec(command); } catch (IOException e) { e.printStackTrace(); } \/\/\u5b89\u88c5\u8df3\u8f6c Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); \/\/\u8fd9\u5757\u4e0d\u8981\u5199\u9519\u4e86\uff0c\u4e00\u5b9a\u8981\u662f\u4f60\u81ea\u5df1Manifest\u6ce8\u518c\u7684fileprovider Uri apkUri = FileProvider.getUriForFile(context, \"com.zidian.qingframe.fileprovider\", apkPath); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, \"application\/vnd.android.package-archive\"); } else { intent.setDataAndType(Uri.fromFile(apkPath), \"application\/vnd.android.package-archive\"); } context.startActivity(intent); }<\/code><\/pre>\n
<uses-permission android:name=\"android.permission.REQUEST_INSTALL_PACKAGES\" \/><\/code><\/pre>\n
\u603b\u7ed3<\/h3>\n