Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to handle Android IntentFilter to open app with specific file-types?

$
0
0

How do I handle Android IntentFilter ActionView in a Xamarin.Forms-App?

Here is my FormsApplicationActivity. IntentFilter is defined to enable the app to open *.gpx files from web-pages or file.
The problem is, that whenever a gpx-file opened, OnCreate() is called and a new instance of the app is created, even if the app is already started.

What I actually want, is that the app is just activated, if its already running or started, if not, and then to call OpenUrl() to process the file.
I tried different LaunchMode but no success.

What is the correct way?

    using System;

    using Android.App;
    using Android.Content.PM;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;

    namespace QvmAndroid
    {

        [Activity(Theme = "@style/SplashQVM", MainLauncher = true, NoHistory = true,
                          ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)]

        [IntentFilter(new[] { Intent.ActionView }, DataScheme = "http", DataPathPattern = ".*\\\\.gpx", DataHost = "*", Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, Icon = "@drawable/qvmicon")]
        [IntentFilter(new[] { Intent.ActionView }, DataScheme = "https", DataPathPattern = ".*\\\\.gpx", DataHost = "*", Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, Icon = "@drawable/qvmicon")]
        [IntentFilter(new[] { Intent.ActionView }, DataScheme = "file", DataPathPattern = ".*\\\\.gpx", DataHost = "*", Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, Icon = "@drawable/qvmicon")]

        public class App : Xamarin.Forms.Platform.Android.FormsApplicationActivity
        {

            protected override void OnCreate(Bundle bundle)
            {

                // remove splashscreen, activate normal theme
                // must call BEFORE base.OnCreeate
                //base.SetTheme(global::Android.Resource.Style.ThemeHoloLight);
                SetTheme(Resource.Style.QvmLight);

                base.OnCreate(bundle);

                global::Xamarin.Forms.Forms.Init(this, bundle)

                 var appXf = new QvmXF.App();
                 LoadApplication(appXf);

                OpenUrl();
            }

            void OpenUrl()
            {
                if (Intent != null)
                {
                    // handle file opened in other app 
                    string action = Intent.Action;
                    if (action == Intent.ActionView)
                    {
                        string file = Intent.DataString;
                        Console.WriteLine("---- OpenUrl " + file);
                        if (!string.IsNullOrEmpty(file))
                        {
                            file = file.Replace(@"file://", "");
                            Console.WriteLine("---- OpenUrl " + file);

                        }
                    }
                }
            }
        }
    }

Viewing all articles
Browse latest Browse all 91519

Trending Articles