[英]Android API 30 : How to install packages as App Self-Update
我發現了很多關於這個主題的線程,但它們已經超過 5 年了,並且使用的機制目前不再起作用,所以我決定重新打開這個問題以獲取更新成功開始的要求是什么最新 (Android 30) api。
我為此做的准備:
使用的操作系統:Android API 30 Android Studio:最新存儲庫:Nexus 3
我創建了一個簡單的 MainActivity 作為布局,其中包含一個帶有一個簡單按鈕的 RelativLayout,該按鈕執行 onClick 和 AsyncTask(檢查更新邏輯)。 此任務向我的 nexus 3 發出一個簡單的 rest 請求,以獲取我的應用程序的最新版本。 幾秒鍾后單擊相同的按鈕(只需在第一次單擊完成后設置一個新的偵聽器),它將使用以下代碼繼續安裝:
public void installUpdate(String url,String fileName) {
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
destination += fileName;
updateDestination = destination;
final Uri uri_current = Uri.parse("file://" + destination);
File file = new File(destination);
if (file.exists()) {
file.delete();
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.addRequestHeader("Authorization", "Basic simplebase64tokenherefornexus");
request.setDescription("App Update");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.setTitle(fileName);
request.setDestinationUri(uri_current);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
if (!getPackageManager().canRequestPackageInstalls()) {
startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234);
} else {
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.test.fileprovider", new File(file.getAbsolutePath(), fileName));
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
}
finish();
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
一切正常,我的應用程序的“從未知來源安裝”也被正確調用並激活。 兩個文件(作為發行版安裝的 apk)和作為發行版的更新 apk 都包含相同的 v1 + v2 簽名密鑰。 下載的apk沒有損壞,用manuell安裝作為更新檢查了它,它工作正常,谷歌播放保護也被禁用。
我認為我也設置了正確的權限,在我的清單中我使用了以下權限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:allowBackup="true"
android:testOnly="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider> ...
我的provider_path:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
如果我使用當前配置嘗試它,我仍然會收到“解析包時出現問題”。
我是否錯過了什么或做錯了什么,或者是否有人有最佳實踐示例來讓它發揮作用?
對於那些遇到同樣問題的人:
發現我的問題,失敗是我將安裝權限設置為錯誤的意圖@廣播 xD
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.