android 软件安装-更新-卸载

感谢:[url]http://blog.csdn.net/netpirate/article/details/5801379[/url]


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

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

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


安装程序的方法:

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源提供的安装程序,如果没有打钩的话,系统安装应用时会提示用户设置,如果选择设置,设置好后,无法返回安装界面;如果选择取消,则推出安装程序。所以,如果是更新的话,一定要在下载之前就检测许可安装源的设置,或者在下载前检测是否已经下载过新的安装程序,避免重复下载安装程序。



相关的代码如下:

  int result = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);      

if (result == 0) {

// show some dialog here

// ...

// and may be show application settings dialog manually

Intent intent = new Intent();

intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);

startActivity(intent);

}



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。



下面是程序更新的几个步骤演示:


[img]http://dl.iteye.com/upload/attachment/545849/fc55e78f-4e76-3bc3-a30e-8e4c07557c53.png[/img]

[img]http://dl.iteye.com/upload/attachment/545851/a13b4aef-1d18-31ca-81d2-f82fe36075b0.png[/img]

[img]http://dl.iteye.com/upload/attachment/545853/1297dc7f-da16-3e08-b1fd-ecbdb0bd1f4f.png[/img]

[img]http://dl.iteye.com/upload/attachment/545855/61eda8c9-1905-3671-bacb-c5a3c96c659e.png[/img]


OVER!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值