作者:徐建祥(netpirate@gmail.com)
日期:2010/07/28
網(wǎng)址:http://www.anymobile.org
安裝程序:軟件從無到有。
卸載程序:軟件從有到無。
更新程序:軟件的覆蓋安裝,可以保留原版本的數(shù)據(jù),提升軟件版本。
安裝程序的方法:
1、
通過Intent機制,調(diào)出系統(tǒng)安裝應(yīng)用,重新安裝應(yīng)用的話,會保留原應(yīng)用的數(shù)據(jù)。
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、
直接調(diào)用安裝接口。
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);
安裝應(yīng)用權(quán)限:android.permission.INSTALL_PACKAGES
系統(tǒng)應(yīng)用(安裝在/system/app下面)可以采用該方式,第三方應(yīng)用無法申請安裝卸載權(quán)限。
java.lang.SecurityException: Neither user
10039 nor current process has android.permission.INSTALL_PACKAGES.
3、
執(zhí)行install命令。
install –r 更新安裝,默認(rèn)新安裝;如果不附上-r參數(shù),則會清楚原應(yīng)用的數(shù)據(jù),版本一致則無法安裝。
(1)am start …
(2)Runtime.exec(String[] args)
(3)Class<?> execClass =
Class.forName("android.os.Exec");
4、
執(zhí)行cp / adb push命令。
由系統(tǒng)檢測到應(yīng)用程序有更新,自動完成重新安裝。
5、
通過第三方軟件實現(xiàn)。
Market,EOE,eTrackDog均采用第一種方法實現(xiàn)更新。
優(yōu)點:由系統(tǒng)核心應(yīng)用程序控制安裝程序;
缺點:無法控制安裝過程;安裝完成后,也無法立刻啟動應(yīng)用,需要用戶確認(rèn);無法擴展。
實例:Market查找安裝程序
Intent intent =
new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:your.app.id"));
startActivity(intent);
卸載程序的方法:
1、
通過Intent機制,調(diào)出系統(tǒng)卸載應(yīng)用。
Uri packageURI = Uri.parse("package: your.app.id");
Intent intent = new Intent(Intent.ACTION_DELETE);
startActivity(intent);
2、
直接調(diào)用卸載接口。
PackageInstallObserver observer
= new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags);
卸載應(yīng)用權(quán)限:android.permission.DELETE_PACKAGES
3、
運行rm apk安裝文件,由系統(tǒng)檢測后調(diào)用卸載應(yīng)用。
備注說明:
Android系統(tǒng)的應(yīng)用安裝,在系統(tǒng)設(shè)置里面有一項,是否安裝未知源,所在在軟件更新的時候,需要檢測這個選項,如果打鉤,則只允許安裝Market源提供的安裝程序,如果沒有打鉤的話,系統(tǒng)安裝應(yīng)用時會提示用戶設(shè)置,如果選擇設(shè)置,設(shè)置好后,無法返回安裝界面;如果選擇取消,則推出安裝程序。所以,如果是更新的話,一定要在下載之前就檢測許可安裝源的設(shè)置,或者在下載前檢測是否已經(jīng)下載過新的安裝程序,避免重復(fù)下載安裝程序。
相關(guān)的代碼如下:
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。
下面是程序更新的幾個步驟演示:




OVER!
posted on 2010-08-10 14:00
Xu Jianxiang 閱讀(19477)
評論(6) 編輯 收藏 所屬分類:
Android