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

简介:
 

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

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

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

 

安装程序的方法:

 

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。

相关文章
|
20天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
41 0
|
21天前
|
安全 Android开发
Android 预置可卸载分区接收不到任何广播问题分析和解决
Android 预置可卸载分区接收不到任何广播问题分析和解决
31 0
|
4天前
|
存储 Java 开发工具
在Eclipse配置安装Android详细教程(新手)
在Eclipse配置安装Android详细教程(新手)
15 1
|
4天前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
|
4天前
|
开发工具 Windows
Android4.0安装时需要更新软件包(原创)
Android4.0安装时需要更新软件包(原创)
14 3
|
6天前
|
Java Android开发
Android零基础入门(二)gradle的安装和详解
Android零基础入门(二)gradle的安装和详解
14 0
|
6天前
|
Oracle Java 关系型数据库
Android零基础入门(一)配置环境和安装Android Studio
Android零基础入门(一)配置环境和安装Android Studio
14 0
|
6天前
|
Android开发
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
12 0
|
19天前
|
缓存 Android开发 开发者
pc上使用命令给android安装apk
pc上使用命令给android安装apk
19 0
|
20天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
24 0