閱讀710 返回首頁    go 技術社區[雲棲]


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。

最後更新:2017-04-02 06:51:45

  上一篇:go Android 應用程序窗體顯示狀態操作(顯示隱藏標題欄或自定義窗體大小)
  下一篇:go WCF概要