当前位置: 代码迷 >> Android >> android 软件装配-更新-卸载
  详细解决方案

android 软件装配-更新-卸载

热度:486   发布时间:2016-05-01 17:20:37.0
android 软件安装-更新-卸载
感谢:http://blog.csdn.net/netpirate/article/details/5801379


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

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

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


安装程序的方法:

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.NameValueTablepublic 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!
  相关解决方案