日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中下载、安装和卸载(原)

發布時間:2023/12/18 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中下载、安装和卸载(原) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

應用場景:在檢查版本更新的時候經常需要從服務器端下載然后安裝到手機中

使用工具: XUtils,這個開源的框架真的是需要花大把時間去閱讀和理解的,十分有用的,on the way ! fighting!

下載:github中關鍵字搜索即可download

 

1 //google提供的保留地址,不會隨著電腦ip地址的變化而改變測試ip地址 2 private static final String mDownloadUrl= "http://10.0.2.2:8080/xxx.apk"; 3 4 protected void downloadApk() { 5 6 if (Environment.getExternalStorageState().equals( 7 Environment.MEDIA_MOUNTED)) { 8         //下載apk到sd的路徑 9 String sdPath = Environment.getExternalStorageDirectory() 10 .getAbsolutePath() + File.separator + "xxx.apk"; 11 HttpUtils httpUtils = new HttpUtils(); 12 13 httpUtils.download(mDownloadUrl, sdPath, 14 new RequestCallBack<File>() { 15 16 @Override 17 public void onStart() { 18 Log.i(tag, "開始下載"); 19 super.onStart(); 20 } 21 22 @Override 23 public void onLoading(long total, long current, 24 boolean isUploading) { 25 Log.i(tag, "正在下載中"); 26 Log.i(tag, "total = " + total); 27 Log.i(tag, "current = " + current); 28 super.onLoading(total, current, isUploading); 29 } 30 31 @Override 32 public void onSuccess(ResponseInfo<File> responseInfo) { 33 Log.i(tag, "下載完成"); 34 //獲取下載好的文件路徑 35 File file = responseInfo.result; 36                 //安裝apk 37 installApk(file); 38 39 } 40 41 @Override 42 public void onFailure(HttpException error, String msg) { 43 Log.i(tag, "下載失敗"); 44 } 45 46 }); 47 } 48 } View Code

?

通過隱式意圖去實現下載

  通過查詢源碼把下面代碼摳出來

?

/** <intent-filter> <action android:name="android.intent.action.VIEW" />* <category android:name="android.intent.category.DEFAULT" />
* <data android:scheme="content" /> <data android:scheme="file" />
* <data android:mimeType="application/vnd.android.package-archive" />* </intent-filter>*/

?

 然后就是一個簡單的隱式意圖開啟Activity的過程

?

protected void installApk(File file) {Intent intent = new Intent();intent.setAction("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");/** intent.setData(Uri.fromFile(file));* intent.setType("application/vnd.android.package-archive");*/
          //與被注釋代碼等價,但推薦使用下述方法,被注釋方法存在一個finish問題intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");startActivity(intent);}

?

 //卸載應用的方法同上,找到對應的源碼,查看安卓系統是怎么實現的

?

?

<intent-filter><action android:name="android.intent.action.VIEW" /><action android:name="android.intent.action.DELETE" /><category android:name="android.intent.category.DEFAULT" /><data android:scheme="package" /> </intent-filter>

//同上,開啟意圖即可

protected void uninstall(){ Intent intent = new Intent();intent.setAction(Intent.ACTION_DELETE);
     intent.addCategory("android.intent.category.DEFAULT");intent.setData(Uri.parse("package:"+包名));startActivity(intent); }

  

?

轉載于:https://www.cnblogs.com/adv-qbj/p/4715274.html

總結

以上是生活随笔為你收集整理的Android中下载、安装和卸载(原)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。