android下载安装APK
代码安装apk
- Intent i = new Intent(Intent.ACTION_VIEW);
- String filePath = "/sdcard/XXX.apk";
- i.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");
- context.startActivity(i);
程序卸载
- Intent intent = new Intent(Intent.ACTION_DELETE, uri);
- intent.startActivity();
监听是否卸载成功
- DeleteReceiver mDeleteReceiver = new DeleteReceiver();//自定义的广播接收类,接收到结果后的操作
- IntentFilter filter = new IntentFilter(
- Intent.ACTION_PACKAGE_DATA_CLEARED);
- filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
- filter.addDataScheme("package");
- registerReceiver(mDeleteReceiver, filter); //注册广播和过滤
- Intent undeleteIntent = new Intent(Intent.ACTION_DELETE, packageURI);
- startActivity(undeleteIntent);
文件下载
DownloadProvider的权限级别改成 normal了就可以使用了 网上也有说明
首先要在AndroidManifest.xml中申请访问DownloadManager的权限
- <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
添加一个下载任务:
- ContentValues values = new ContentValues();
- values.put(Downloads.URI, url);//指定下载地址
- values.put(Downloads.COOKIE_DATA, cookie);//如果下载Server需要cookie,设置cookie
- values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//设置下载提示 是否在屏幕顶部显示
- values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//设置下载完成之后回调的包名
- values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//设置下载完成之后负责接收的Receiver,这个类要继承 BroadcastReceiver
- values.put(Downloads.DESTINATION,save_path);//设置下载到的路径,这个需要在Receiver里 自行处理
- values.put(Downloads.TITLE,title);//设置下载任务的名称
- this.getContentResolver().insert(Downloads.CONTENT_URI, values);//将其插入到DownloadManager的数据库中,数据库会触发修改事件,启动下载任务
- ContentValues values = new ContentValues();
- values.put("uri", uri.toString());
- values.put("useragent", "Mozilla/5.0 (Linux; U; Android 1.5; en-us; sdk Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1");
- values.put("notificationpackage", getPackageName());
- values.put("notificationclass", "HelloWorld");
- values.put("visibility", 1);
- values.put("mimetype", mimetype);
- values.put("hint", filename);
- values.put("description", uri.getHost());
- values.put("total_bytes", 1349528);
- mResolver = getContentResolver();
- mResolver.insert(Uri.parse("content://downloads/download"), values);