以編程方式安裝/卸載APK(PackageManager vs意圖)我的應(yīng)用程序安裝了其他應(yīng)用程序,它需要跟蹤它安裝了哪些應(yīng)用程序。當(dāng)然,這可以通過簡單地保存已安裝應(yīng)用程序的列表來實現(xiàn)。但這不應(yīng)該是必要的!PackageManager應(yīng)該負(fù)責(zé)維護(hù)installedBy(a,b)關(guān)系。實際上,根據(jù)API,它是:公共抽象字符串getInstallerPackageName(StringPackageName)-檢索安裝包的應(yīng)用程序的包名。這將識別該軟件包來自哪個市場?,F(xiàn)行方法使用意向安裝APKIntent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);使用意圖卸載APK:Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).
packageName,null));startActivity(intent);這顯然不是Android Market安裝/卸載軟件包的方式。他們使用更豐富版本的PackageManager。這可以通過從AndroidGit存儲庫下載Android源代碼來看到。下面是兩個與意圖方法相對應(yīng)的隱藏方法。不幸的是,外部開發(fā)人員無法使用它們。但也許他們會在未來?更好的方法使用PackageManager安裝APK/**
* @hide
*
* Install a package. Since this may take a little while, the result will
* be posted back to the given observer. An installation will fail if the calling context
* lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
* package named in the package file's manifest is already installed, or if there's no space
* available on the device.
*
* @param packageURI The location of the package file to install. This can be a 'file:' or a
* 'content:' URI.
* @param observer An observer callback to get notified when the package installation is
* complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
* called when that happens. observer may be null to indicate that no callback is desired.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that is performing the差異當(dāng)使用意圖時,本地包管理器不知道安裝來自哪個應(yīng)用程序。具體來說,getInstallerPackageName(.)返回NULL。隱藏方法安裝包(.)將安裝程序包名稱作為參數(shù),并且最有可能設(shè)置此值。問題是否可以使用intents指定包安裝程序名稱? (也許可以在安裝意圖之外添加安裝程序包的名稱?)提示:如果您想下載Android源代碼,可以按照這里描述的步驟:下載源代碼樹。要提取*.java文件并根據(jù)包層次結(jié)構(gòu)將它們放在文件夾中,您可以查看這個整潔的腳本:查看Eclipse中的Android源代碼.
3 回答

德瑪西亞99
TA貢獻(xiàn)1770條經(jīng)驗 獲得超3個贊
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
Intent intent = new Intent(Intent.ACTION_DELETE);intent.setData(Uri.parse("package:com.example.mypackage"));startActivity(intent);
- 3 回答
- 0 關(guān)注
- 1325 瀏覽
添加回答
舉報
0/150
提交
取消