Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1541298
  • 博文数量: 237
  • 博客积分: 5139
  • 博客等级: 大校
  • 技术积分: 2751
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 14:48
文章分类

全部博文(237)

文章存档

2016年(1)

2012年(4)

2011年(120)

2010年(36)

2009年(64)

2008年(12)

分类: Java

2011-05-17 12:59:03

本文转自:

 http://blog.csdn.net/a54cc/archive/2010/11/08/5994641.aspx



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

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

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

 

安装程序的方法:

 

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   

public static final   INSTALL_NON_MARKET_APPS

Since:   

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 

 

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

 

 

 

阅读(599) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~