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




OVER!
posted on 2010-08-10 14:00
Xu Jianxiang 閱讀(19479)
評(píng)論(6) 編輯 收藏 所屬分類(lèi):
Android