Forum Xamarin.Android

App Link in Android

I am trying add an app link so another application can launch and pass some data to my app. I have it working on my Xamarin IOS app, but can't seem to get it to work on android. The url/link in IOS is MyTestPhoneApp://testdatahere . I am guessing I missing a step or have something wrong. Not trying to open from web url.

[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "MyTestPhoneApp",
DataHost = "testdatahere")]
Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    In xamarin forms ,if you want to launching app through URI , you can refer to the following method:

    1.create an interface for the OpenAppService

    public interface IOpenAppService
    {
        Task<bool> Launch(string stringUri);
    }
    

    2.OpenAppService (Droid)

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.Droid
    {
        public class OpenAppService : Activity, IOpenAppService
        {
            public Task<bool> Launch(string stringUri)
            {
            try{
                Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
    
    
                if (intent != null)
                {
                    intent.AddFlags(ActivityFlags.NewTask);
                    Forms.Context.StartActivity(intent); 
                }
                else
                {
                    intent = new Intent(Intent.ActionView);
                    intent.AddFlags(ActivityFlags.NewTask);
                    intent.SetData(Android.Net.Uri.Parse("market://details?id=" + stringUri)); // This launches the PlayStore and search for the app if it's not installed on your device
                    Forms.Context.StartActivity(intent);
                }
                    return Task.FromResult(true);
              }
                return Task.FromResult(false);
            }
     }
    

    3.OpenAppService (iOS)

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.iOS
    {
        public class OpenAppService : IOpenAppService
        {
    
            public Task<bool> Launch(string stringUri)
            {
                try
                {
                    NSUrl request = new NSUrl(stringUrl);
                    bool isOpened = UIApplication.SharedApplication.OpenUrl(stringUrl);
    
                    if (isOpened == false)
                        UIApplication.SharedApplication.OpenUrl(new NSUrl(stringUrl));
    
                    return Task.FromResult(true);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
    
                    alertView.Show();
    
                    return Task.FromResult(false);
                }
            }
        }
    

    For more details, you can check:
    https://mindofai.github.io/Launching-Apps-thru-URI-with-Xamarin.Forms/
    https://forums.xamarin.com/discussion/comment/251209/#Comment_251209

  • RobAtKansysRobAtKansys Member ✭✭

    @jezh said:
    In xamarin forms ,if you want to launching app through URI , you can refer to the following method:

    1.create an interface for the OpenAppService

    public interface IOpenAppService
    {
        Task<bool> Launch(string stringUri);
    }
    

    2.OpenAppService (Droid)

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.Droid
    {
        public class OpenAppService : Activity, IOpenAppService
        {
            public Task<bool> Launch(string stringUri)
            {
            try{
                Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
    
                
                if (intent != null)
                {
                    intent.AddFlags(ActivityFlags.NewTask);
                    Forms.Context.StartActivity(intent); 
                }
                else
                {
                    intent = new Intent(Intent.ActionView);
                    intent.AddFlags(ActivityFlags.NewTask);
                    intent.SetData(Android.Net.Uri.Parse("market://details?id=" + stringUri)); // This launches the PlayStore and search for the app if it's not installed on your device
                    Forms.Context.StartActivity(intent);
                }
                    return Task.FromResult(true);
              }
                return Task.FromResult(false);
            }
     }
    

    3.OpenAppService (iOS)

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.iOS
    {
        public class OpenAppService : IOpenAppService
        {
          
            public Task<bool> Launch(string stringUri)
            {
                try
                {
                    NSUrl request = new NSUrl(stringUrl);
                    bool isOpened = UIApplication.SharedApplication.OpenUrl(stringUrl);
    
                    if (isOpened == false)
                        UIApplication.SharedApplication.OpenUrl(new NSUrl(stringUrl));
                        
                    return Task.FromResult(true);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
    
                    alertView.Show();
                    
                    return Task.FromResult(false);
                }
            }
        }
    

    For more details, you can check:
    https://mindofai.github.io/Launching-Apps-thru-URI-with-Xamarin.Forms/
    https://forums.xamarin.com/discussion/comment/251209/#Comment_251209

    I am not trying to launch another app. I am trying to make it possible to another app to launch my app. I already have it working on the ios app using url types and can launch my app via "MyTestPhoneApp://testdatahere". But I can't find a way to implement this in android. Have some some examples but none seem to work.

  • jezhjezh Member, Xamarin Team Xamurai

    It doesn't matter if another app launches your app, or if your app launches another app, it's the same thing.
    In other words, the other apps should achieve the function of lauching your app.

  • RobAtKansysRobAtKansys Member ✭✭

    @jezh said:
    It doesn't matter if another app launches your app, or if your app launches another app, it's the same thing.
    In other words, the other apps should achieve the function of lauching your app.

    I understand, but there is no place in the example code setting up the uri that my app will be listening on, the "MyTestPhoneApp://testdatahere" that the other app will call. I already have working code to launch another app, it works and is in production. Now I need the code to listen and let other apps launch my app. The code above launches other apps.

Sign In or Register to comment.