Hello. I have .net maui application that serves as launcher for another multiplatform application. This launcher should install/update that another application and then do some actions (update ui, delete temporary files,..). The problem is that I can't find a way to run such a callback.
In my app DownloadCompletedCallback runs after downloading all necessary files:
private void DownloadCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
try
{
ZipFile.ExtractToDirectory(zipPath, cachePath, true);
File.Delete(zipPath);
InstallApp install = new InstallApp(apkPath, versionRef, ref VersionLabel);
install.Run();
}
catch (Exception ex)
{
StatusLabel.Text = ($"Error installing or updating app: {ex}");
}
}
This method extracts all files (apk in case of Android) and then handles installation via partial InstallApp class. Run method of this class executes installation and it has different implemintations depending on specific platform (I used the approach described in docs: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/invoke-platform-code?view=net-maui-8.0). In case of Android the method looks like this:
partial class InstallApp
{
partial void _Run()
{
var context = Android.App.Application.Context;
Java.IO.File apkFile = new Java.IO.File(_apkPath);
Intent intent = new Intent(Intent.ActionView);
var uri = Microsoft.Maui.Storage.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", apkFile);
intent.SetDataAndType(uri, "application/vnd.android.package-archive");
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra(Intent.ExtraNotUnknownSource, true);
Platform.CurrentActivity.StartActivity(intent);
}
}
}
Here I am using StartActivity method. Problems start here because I want to get access to callback function after successful apk installation. There are two ways do that as I found:
- Using StartActivityForResult and onActivityResult callback. In that case I need to call StartActivityForResult instead of StartActivity with some code:
Platform.CurrentActivity.StartActivityForResult(intent, 1);
And then I need to override the callback and catch the end of my activity via requestCode:
protected override void OnActivityResult(int requestCode, int resultCode, Android.Content.Intent intent)
{
if (requestCode == 1)
{
// some actions after installing/updating the app
WebClient webClient = new WebClient();
Version remoteVersion = new Version(webClient.DownloadString(_versionRef));
_VersionLabel.Text = remoteVersion.ToString();
File.Delete(_apkPath);
}
}
The method can be overridden only in case when InstallApp class inherits Action class from Android.App. But InstallApp is a partial class that shared between different platforms (not only Android), so it can't inherit from Android specific class.
- Using RegisterForActivityResult method from Android Activity Result API. The problem is that I can't find the way to import this method in android specific section of my app in order to use it inside of my InstallApp class methods. I found not too much about this case and it didn't help:https://stackoverflow.com/questions/74199643/cant-find-registerforactivityresult-in-xamarin-maui-c-sharp-libraries
I am really stuck on this problem and can't move forward, so I will appreciate any help!