.NET MAUI: popup modal window cancels an intent (Android platform)

rodionovid 80 Reputation points
2024-03-13T22:21:12.66+00:00

Hello. I am writing .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,..). Another question about the app I asked here: https://learn.microsoft.com/en-us/answers/questions/1611416/net-maui-cant-run-activityresult-callback-on-andro

During apk installation the modal window appears:

photo_2024-03-14_00-41-21

and clicking to "Settings" button application settings screen appears:

photo_2024-03-14_00-41-30

The appearance of this modal window interrupts the apk installation. From OnActivityResult callback I figured out that my intent finishes with resultCode = "Canceled".

Method that perfoms apk installation and includes the intent looks like this:

    public 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);
            intent.PutExtra("apkPath", _apkPath);

            MainActivity.Instance.StartActivityForResult(intent, 1);
        }
    }


I would like to know if there is any way to handle this modal window and continue the intent instead of canceling it? Or maybe there are exists another common way to handle this issue?

I will appreciate any help with this issue.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,036 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,096 Reputation points Microsoft External Staff
    2024-03-14T06:44:19.58+00:00

    Hello,

    Before you use Intent to install apk, you can do this by adding following permission in the AndroidManifest.xml

        <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    

    Then, you need to check your application if could install unknown app by var res=packageManager.CanRequestPackageInstalls();. If not, you can start Activity to enable this permission by users for your application.

    #if ANDROID
                var packageManager=Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.PackageManager;
                var res=packageManager.CanRequestPackageInstalls();
                if(!res) {
                    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionManageUnknownAppSources, Android.Net.Uri.Parse("package:" + AppInfo.Current.PackageName)));
               }
    #endif
    

    In the end, you can execute InstallApp._Run(); if user enable the PackageInstalls permission.

    #if ANDROID
     var packageManager=Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.PackageManager;
                var res=packageManager.CanRequestPackageInstalls();
                if(res) {
                    InstallApp._Run();
    }
    
    #endif
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.