码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-08-09 16:52:08      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

应用场景:在检查版本更新的时候经常需要从服务器端下载然后安装到手机中

使用工具: 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.setData(Uri.parse("要卸载的应用程序包名"));
	startActivity(intent);
}

  

 

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

标签:

原文地址:http://www.cnblogs.com/adv-qbj/p/4715274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!