I want to install a 3rd party app from the filesystem from my xamarin android app. The code I used successfully before Android 10 was pretty straightforward and easy.
Intent intent = new Intent(Intent.ActionView);
Uri data = Uri.FromFile(file);
intent.SetDataAndType(data, "application/vnd.android.package-archive"); context.StartActivity(intent);
This code does not work on Android 10 because of ACTION_VIEW and ACTION_INSTALL_PACKAGE were deprecated in Android 10. Looks like we now need to use the PackageInstaller API.
I tried to write a method using the PackageInstaller API. Unfortunately it doesn't work.
Code with PackageInstaller API
public static void InstallPackageAndroidQAndAbove(Context context, string filePath, string packageName)
{
var packageInstaller = context.PackageManager.PackageInstaller; var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall); sessionParams.SetAppPackageName(packageName); int sessionId = packageInstaller.CreateSession(sessionParams); var session = packageInstaller.OpenSession(sessionId); var input = new FileStream(filePath, FileMode.Open, FileAccess.Read); var output = session.OpenWrite(packageName, 0, -1); input.CopyTo(output); output.Close(); input.Close(); input.Dispose(); session.Fsync(output); var pendingIntent = PendingIntent.GetBroadcast(context, sessionId, new Intent(Intent.ActionInstallPackage), 0); session.Commit(pendingIntent.IntentSender); }
An exception "Unrecognized stream" occurs during the call.
I hope someone can help me.
Thank you very much in advance.
Try the following code about installing using PackageInstaller.
PackageInstaller packageInstaller = Application.Context.PackageManager.PackageInstaller; PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall); sessionParams.SetAppPackageName("packageName"); int sessionId = packageInstaller.CreateSession(sessionParams); PackageInstaller.Session session = packageInstaller.OpenSession(sessionId); Intent intent = new Intent(); intent.SetAction("ACTION_INSTALL_COMPLETE"); PendingIntent pendingIntent = PendingIntent.GetBroadcast( Application.Context, sessionId, intent, 0); session.Commit(pendingIntent.IntentSender);
Answers
nobody knows?
ACTION_INSTALL_PACKAGE
is deprecated in API level 29, you should use PackageInstaller instead.Check the Tutorial:
https://developer.android.com/reference/android/content/Intent#ACTION_INSTALL_PACKAGE
Ok thank you. I found that and tried to adapt my code.
In my example code I am using the PackageInstaller but it is not working and i am kind of lost.
Is there any working xamarin code around?
Try the following code about installing using PackageInstaller.
Refer to:
https://stackoverflow.com/questions/26884956/how-to-install-update-remove-apk-using-packageinstaller-class-in-android-l
Jarvan thank you very much for your help! I was able to solve the issue yesterday evening.
I have successfully adapted the code from the InstallApkSessionApi.java example.
Xamarin Android Code for installing an apk with the PackageInstaller API.
Thanks smedasn, 1 question. Where do you put your apk file ? Before Q I use Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)
but this depreceated and if I understand it right only App-private directorys are useable for non Mediafiles (Foto/Video/Audio) Right now I use android:requestLegacyExternalStorage="true" in the manifest for Q-Devices.