Launcher.Default.TryOpenAsync() not working in MAUI App

492 views Asked by At

When calling Launcher.Default.TryOpenAsync(), its always return false in MAUI app. We need to open an installed app in our device from a button click. If not installed, it should go tor play store for download.

Tried this code, but not working

private async void Button1_ClickAsync(object sender, EventArgs e)
{
try
{   string package_name = txtPackagename.Text;   

    // bool supportsUri = await Launcher.Default.CanOpenAsync("com.google.android.youtube") // not working...

    // Try to open You tube App installed.
    bool launcherOpened = await Launcher.Default.TryOpenAsync("com.google.android.youtube");

    if (launcherOpened)
    {
        await DisplayAlert("Done", "Application Successfully Open.", "OK");
    }
}
catch (Exception ex)
{
    await DisplayAlert("No Support", ex.Message, "OK");
} 
}

Always its return false, even if the app is installed. How we can achieve this in MAUI? In Android studio, we can open an app with a package name.

Android Studio Code(this code working in Android Studio Project)

public void openApplication(Context context, String packageN) {
Intent i = context.getPackageManager().getLaunchIntentForPackage(packageN);
if (i != null) {
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    context.startActivity(i);
} else {
    try {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
    }
    catch (android.content.ActivityNotFoundException anfe) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
    }
} }
1

There are 1 answers

33
Jessie Zhang -MSFT On BEST ANSWER

Suppose the package name of the app you want to open is com.xamarin.secondapp.

Then you need to add IntentFilter and Exported =true to MainActivity.cs of the app you want to open(com.xamarin.secondapp).

Just as follows:

  [Activity(Label = "SecondApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "myapp" }
    )
]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }

Note: remember to add code DataSchemes = new[] { "myapp" }.

For the current app, you need to add queries tag for the app you want to open in file manifest.xml.

For example:

  <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.openappapp1">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
    <application android:label="OpenappApp1.Android" android:theme="@style/MainTheme">

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


      <queries>
            <package android:name="com.xamarin.secondapp" />
      </queries>
</manifest>

And usage example:

public async void OpenSecondApp()
{

    var supportsUri = await Launcher.Default.CanOpenAsync("myapp://");
    if (supportsUri)
        await Launcher.Default.OpenAsync("myapp://com.xamarin.secondapp");

}