Open Google Wallet Offline using a Button Click

Chathura Galkandage 0 Reputation points
2023-07-10T13:40:03.4966667+00:00

I am trying to use .Net MAUI Project template to build an Android App to Emulate in Pixel 4 - API33 using Android Device Manager. I tried many things in MainPage.xaml.cs to use the button given in the example code for an additional function. I need the button to Open the Google Wallet App if it is installed. I used many different codes and ended up with following error at the end.

System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'

I used following code block:

using Android.Content;
using Android.Net;
namespace <MyProjectName>;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private async void OnCounterClicked(object sender, EventArgs e)
	{
		count++;

		if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);

		string packageName = "com.google.android.apps.walletnfcrel";
		if (await Launcher.CanOpenAsync(packageName))
		{
            try
            {
                
                Intent intent = new Intent(Intent.ActionView);
                intent.SetData(Android.Net.Uri.Parse("market://details?id=" + packageName));
                intent.SetClassName("com.android.vending", "com.android.vending.AssetBrowserActivity");
                intent.AddFlags(ActivityFlags.NewTask);
                Android.App.Application.Context.StartActivity(intent);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to Open Google Wallet: {ex.Message} ");
            }

        }
        else
        {
            // Google Wallet app is not installed, display an error message or take alternative action
            Console.WriteLine("Google Wallet app is not installed.");
        }

    }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,628 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 64,576 Reputation points Microsoft Vendor
    2023-07-11T05:19:47.6133333+00:00

    Hello,

    You can do it by Android.Content.Intent to open Google wallet application.

    Here is a simple code that you can add it in your Button Click event.

    
    
    #if ANDROID
            Intent intent = new Intent();
            intent.SetPackage("com.google.android.apps.walletnfcrel");
            intent.AddFlags(ActivityFlags.NewTask);
           
            Android.App.Application.Context.StartActivity(intent);
    
    #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.