安裝程式:軟體從無到有。< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />

 

卸載程式:軟體從有到無。
更新程式:軟體的覆蓋安裝,可以保留原版本的資料,提升軟體版本。

 

安裝程式的方法:

 

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機制,調出系統卸載應用。
UripackageURI = 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 classSettings.Secureextends 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。
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()