Android 程序的安装、卸载和更新
来源:互联网 发布:网络销售自我评价 编辑:程序博客网 时间:2024/06/10 20:29
Android 程序的安装、卸载和更新
- 标签:
- android /
- application /
- string /
- delete /
- file /
- api
作者:徐建祥(netpirate@gmail.com)
日期:2010/07/28
网址:http://www.anymobile.org
安装程序:软件从无到有。
卸载程序:软件从有到无。
更新程序:软件的覆盖安装,可以保留原版本的数据,提升软件版本。
安装程序的方法:
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命令。
由系统检测到应用程序有更新,自动完成重新安装。
5、 通过第三方软件实现。
Market,EOE,eTrackDog均采用第一种方法实现更新。
优点:由系统核心应用程序控制安装程序;
缺点:无法控制安装过程;安装完成后,也无法立刻启动应用,需要用户确认;无法扩展。
实例:Market查找安装程序
Intent intent =
new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
startActivity(intent);
卸载程序的方法:
1、 通过Intent机制,调出系统卸载应用。
Uri packageURI = 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安装文件,由系统检测后调用卸载应用。
备注说明:
Android系统的应用安装,在系统设置里面有一项,是否安装未知源,所在在软件更新的时候,需要检测这个选项,如果打钩,则只允许安装Market源提供的安装程序,如果没有打钩的话,系统安装应用时会提示用户设置,如果选择设置,设置好后,无法返回安装界面;如果选择取消,则推出安装程序。所以,如果是更新的话,一定要在下载之前就检测许可安装源的设置,或者在下载前检测是否已经下载过新的安装程序,避免重复下载安装程序。
相关的代码如下:
1. int result = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
2. if (result == 0) {
3. // show some dialog here
4. // ...
5. // and may be show application settings dialog manually
6. Intent intent = new Intent();
7. intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
8. startActivity(intent);
9. }
public static final class Settings.Secure extends Settings.NameValueTable
public static final String INSTALL_NON_MARKET_APPS
Since: API Level 3
Whether the package installer should allow installation of apps downloaded from sources other than the Android Market (vending machine). 1 = allow installing from other sources 0 = only allow installing from the Android Market。
下面是程序更新的几个步骤演示:
OVER!
killerjava
2014-07-22 15:234楼- //卸载APP
public void uninstall(String strPackage) {
Uri packageURI = Uri.parse("package:xxx.xxx.xxx");//package
Intent intent = new Intent(Intent.ACTION_DELETE,packageURI);
startActivity(intent);
} - 回复
maggiccrystal_3
2013-08-27 16:323楼- 、 通过Intent机制,调出系统卸载应用。
Uri packageURI = Uri.parse("package: your.app.id");
Intent intent = new Intent(Intent.ACTION_DELETE);
startActivity(intent);
卸载不了sytem/app目录下面的apk - 回复
feng283797821
2013-01-06 16:572楼- LZ是在茂碧做的?能否透露一下艾米聊天服务端是用什么的吗?目前只是用openfire
- 回复
mshaolin
2011-04-20 01:181楼- [e06]
大侠
pm.installPackage(mPackageURI, observer, installFlags);
这个接口没有啊. - 回复
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新 .
- Android 程序的安装、卸载和更新
- Android程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- Android 程序的安装、卸载和更新
- tcp协议建立连接 为什么是三次握手 而 断开连接需要四次?
- root密码忘记解决方案简述
- HashSet模拟新浪微博用户注册
- error LNK2026: 模块对于 SAFESEH 映像是不安全的
- java8中的filter和removeIf的区别
- Android 程序的安装、卸载和更新
- Pandas 数据集合并
- Ubuntu下安装Java
- C++实战之OpenCL矩阵相乘
- swift 弹幕碰撞检测
- Eclipse导出Maven项目生成war包的两种办法
- Android流式布局
- 海明校验码
- 深入解析js中基本数据类型与引用类型,函数参数传递的区别