安装程序的方法: 1、通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据。 String fileName = Environment.getExternalStorageDirectory() +apkName; Uri uri = Uri.fromFile(new File(fileName)); Intent intent =new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri,"application/vnd.android.package-archive"); startActivity(intent); 复制代码 2、直接调用安装接口。 Uri mPackageURI = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + apkName)); int installFlags = 0; PackageManager pm = getPackageManager(); try { PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); if(pi !=null) { installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE; } } catch (NameNotFoundException e) {} PackageInstallObserver observer =new PackageInstallObserver(); pm.installPackage(mPackageURI, observer, installFlags); 复制代码 安装应用权限:android.permission.INSTALL_PACKAGES 系统应用(安装在/system/app下面)可以采用该方式,第三方应用无法申请安装卸载权限。 java.lang.SecurityException: Neither user 10039 nor current process has android.permission.INSTALL_PACKAGES. 3、执行install命令。 install –r 更新安装,默认新安装;如果不附上-r参数,则会清楚原应用的数据,版本一致则无法安装。 (1)am start … (2)Runtime.exec(String[] args) (3)Class<?> execClass = Class.forName("android.os.Exec"); 4、执行cp / adb push命令。 由系统检测到应用程序有更新,自动完成重新安装。 卸载程序的方法: 1、通过Intent机制,调出系统卸载应用。 UripackageURI = Uri.parse("package: your.app.id"); Intent intent =new Intent(Intent.ACTION_DELETE); startActivity(intent); 复制代码 2、直接调用卸载接口。 PackageInstallObserver observer =new PackageInstallObserver(); pm.installPackage(mPackageURI, observer, installFlags); 复制代码 卸载应用权限:android.permission.DELETE_PACKAGES 3、运行rm apk安装文件,由系统检测后调用卸载应用。
用户可以从market上直接下载下来直接
安装
就可以了,会自动识别覆盖的,除非你把版本调低了
1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2. package="com.xx.xx" 包名
3. android:versionCode="X" 版本号
4. android:versionName="xxx"> 版本名
5.
复制代码
就是修改这个versionCode和versionName
代码安装apk
1. Intent i = new Intent(Intent.ACTION_VIEW);
2. String filePath = "/sdcard/XXX.apk";
3. i.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");
4. context.startActivity(i);
5.
复制代码
程序卸载
1. Intent intent = new Intent(Intent.ACTION_DELETE, uri);
2. intent.startActivity();
3.
复制代码
文件下载
DownloadProvider的权限级别改成 normal了就可以使用了 网上也有说明
首先要在
Manifest.xml中申请访问DownloadManager的权限
1. <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
2.
复制代码
1. ContentValues values = new ContentValues();
2. values.put(Downloads.URI, url);//指定下载地址
3. values.put(Downloads.COOKIE_DATA, cookie);//如果下载Server需要cookie,设置cookie
4. values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//设置下载提示 是否在屏幕顶部显示
5. values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//设置下载完成之后回调的包名
6. values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//设置下载完成之后负责接收的Receiver,这个类要继承 BroadcastReceiver
7. values.put(Downloads.DESTINATION,save_path);//设置下载到的路径,这个需要在Receiver里 自行处理
8. values.put(Downloads.TITLE,title);//设置下载任务的名称
9. this.getContentResolver().insert(Downloads.CONTENT_URI, values);//将其插入到DownloadManager的数据库中,数据库会触发修改事件,启动下载任务
10.
11.
12. ContentValues values = new ContentValues();
13. values.put("uri", uri.toString());
14. 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");
15. values.put("notificationpackage", getPackageName());
16. values.put("notificationclass", "HelloWorld");
17. values.put("visibility", 1);
18. values.put("mimetype", mimetype);
19. values.put("hint", filename);
20. values.put("description", uri.getHost());
21. values.put("total_bytes", 1349528);
22.
23. mResolver = getContentResolver();
24. mResolver.insert(Uri.parse("content://downloads/download"), values);
25.
复制代码