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)));
}
} }
Suppose the package name of the app you want to open is
com.xamarin.secondapp
.Then you need to add
IntentFilter
andExported =true
toMainActivity.cs
of the app you want to open(com.xamarin.secondapp
).Just as follows:
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 filemanifest.xml
.For example:
And usage example: