Android 程序的安装、卸载和更新

本文介绍了Android应用的安装与卸载方法,包括通过Intent机制、直接调用接口、执行命令等方式,并探讨了不同方法的优缺点及权限要求。

安装程序:软件从无到有。

卸载程序:软件从有到无。

更新程序:软件的覆盖安装,可以保留原版本的数据,提升软件版本。

安装程序的方法:

1、通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据。

String fileName = Environment.getExternalStorageDirectory() +apkName;

Uri uri = Uri.fromFile(newFile(fileName));

Intent intent =newIntent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri,application/vnd.android.package-archive");

startActivity(intent);

2、直接调用安装接口。

UrimPackageURI= Uri.fromFile(newFile(Environment.getExternalStorageDirectory() +apkName));

intinstallFlags = 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 =newPackageInstallObserver();

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参数,则会清楚原应用的数据,版本一致则无法安装。

1am start …

2Runtime.exec(String[] args)

3Class<?> execClass = Class.forName("android.os.Exec");

4、执行cp / adb push命令。

由系统检测到应用程序有更新,自动完成重新安装。

5、通过第三方软件实现。

MarketEOEeTrackDog均采用第一种方法实现更新。

优点:由系统核心应用程序控制安装程序;

缺点:无法控制安装过程;安装完成后,也无法立刻启动应用,需要用户确认;无法扩展。

实例:Market查找安装程序

Intent intent =

new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));

startActivity(intent);

卸载程序的方法:

1、通过Intent机制,调出系统卸载应用。

UripackageURI= Uri.parse("package:your.app.id");

Intent intent =newIntent(Intent.ACTION_DELETE,packageURI);

startActivity(intent);

2、直接调用卸载接口。

PackageInstallObserver observer =newPackageInstallObserver();

pm.installPackage(mPackageURI, observer, installFlags);

卸载应用权限:android.permission.DELETE_PACKAGES

3、运行rm apk安装文件,由系统检测后调用卸载应用。

备注说明:

Android系统的应用安装,在系统设置里面有一项,是否安装未知源,所在在软件更新的时候,需要检测这个选项,如果打钩,则只允许安装Market源提供的安装程序,如果没有打钩的话,系统安装应用时会提示用户设置,如果选择设置,设置好后,无法返回安装界面;如果选择取消,则推出安装程序。所以,如果是更新的话,一定要在下载之前就检测许可安装源的设置,或者在下载前检测是否已经下载过新的安装程序,避免重复下载安装程序。

相关的代码如下:

1.intresult=Settings.Secure.getInt(getContentResolver(),Settings.Secure.INSTALL_NON_MARKET_APPS,0);

2.if(result==0){

3.//showsomedialoghere

4.//...

5.//andmaybeshowapplicationsettingsdialogmanually

6.Intentintent=newIntent();

7.intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);

8.startActivity(intent);

9.}

public static final classSettings.SecureextendsSettings.NameValueTable

public static finalStringINSTALL_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

转载自: http://www.blogjava.net/anymobile/articles/328406.html

转载批注:

pm.installPackage方法,安装会遇到权限问题,出现以下log,

E/AndroidRuntime( 4239): java.lang.SecurityException: Neither user 10058 nor current process has android.permission.INSTALL_PACKAGES.

AndroidManifest.xml加上

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

也无效。

应用程序安装通常应用第一种方法完成:

String fileName = Environment.getExternalStorageDirectory() +apkName;

Uri uri = Uri.fromFile(newFile(fileName));

Intent intent =newIntent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri,application/vnd.android.package-archive");

startActivity(intent);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值