私の使用例の1つでは、最初のアプリケーションがサーバーに接続し、同じアプリケーションのアップデートをダウンロードします。MarshmallowでプログラムでAPKをインストール
私はAndroidのDownloadManagerクラスをダウンロードしています。 次に、以下のコードを使用して、onReceive()
にアプリインストーラのインテントを作成しようとします。つまり、APKが正常にダウンロードされた後です。
@Override
public void onReceive(Context context, Intent intent) {
//check if the broadcast message is for our Enqueued download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadReference == referenceId) {
try {
Log.v("", "Downloading of the new app version complete");
String filepath = Environment.getExternalStorageDirectory() + "/" +
Environment.DIRECTORY_DOWNLOADS + "/" + mLatestVersionFileName;
//start the installation of the latest version
Uri uri = downloadManager.getUriForDownloadedFile(downloadReference);
//downloadManager.getUriForDownloadedFile(downloadReference)
Uri fileLoc = Uri.fromFile(new File(filepath));
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
} catch (Exception e) {
e.printStackTrace();
}
}
}
コードはPre-Marshmallowデバイスで正常に動作し、ユーザーはAPKをインストールする画面を表示します。
これはマシュマロデバイスでは機能しません。
私はマシュマロに次の例外を取得しています:
android.content.ActivityNotFoundException://ダウンロード/ my_downloads /:いいえ活動はテント{行為= android.intent.action.VIEW DAT =コンテンツを処理することが判明し287 TYP =アプリケーション/ vnd.android.package-アーカイブFLG = 0x10000000}
私は物事次試してみました:
- の月の正確なPackageInstallerを使用してshmallow
(com.google.android.packageinstaller/com.android.packageinstaller.PackageInstallerActivity)
インテントのコンポーネント名。 - uriの代わりにfileLocを渡します。
promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
これは、 '解析エラー' になります。
理想的なインテントはAndroid OSによって解決されます。私はそれが大きな問題ではないと思っています。