Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

No activity found to handle intent: How to handle activities accross multiple projects?

jb_delvauxjb_delvaux Member ✭✭
edited January 19 in Xamarin.Forms

Greetings,

I am currently working on an app belonging to my company. The app is in two projects, one MVVM and the other the actual app. The actual app does nothing beyond start things, and the MVVM takes over for about everything else.

I've been trying to find a way to update the device, with little success currently. I've finally managed to make sure the device can find the file on the network, but I cannot get it to start the APK. Here's the relevant code.

public async static Task<bool> UpdateToServerVersion()
        {
            bool returnValue = false;
            await Task.Run(() =>
            {
                try
                {
                    SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8080");
                    string path = @"smb://Server/Share/IT/Androids/Picking.apk";
                    var auth = new NtlmPasswordAuthentication("Domain", "Login", "Password");
                    //Get target's SmbFile.
                    var file = new SmbFile(path, auth);
                    //Check if file exist
                    if (file.Exists())
                    {
                        Intent PromptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.Parse(file.GetPath()), "application/vnd.android.package-archive");
                        PromptInstall.SetFlags(ActivityFlags.GrantReadUriPermission);
                        PromptInstall.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(PromptInstall);
                    }
                    else
                    {
                        returnValue = false;
                    }
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.ToString());
                    returnValue = false;
                }
            });
            return returnValue;
        }

When it comes to the two projects, here is an image to show what it looks like:

The code shown above is within the "Helpers => common" from the Picking project, whereas the MainActivity is in Picking.Android. I presume the error I have is to do with it and prevent me from starting the intent.

Finally, here's the error returned when I try to run the code above:

No Activity found to handle Intent { act=android.intent.action.VIEW dat=smb://Domain/Share/IT/Androids/Picking.apk typ=application/vnd.android.package-archive flg=0x10000000 }01-19 17:16:22.236 I/mono-stdout(12860): Android.Content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=smb://Domain/Share/IT/Androids/Picking.apk typ=application/vnd.android.package-archive flg=0x10000000 }

at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in :0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in :0
at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00031] in :0
at Picking.Helpers.Common+<>c__DisplayClass46_0.b__0 () [0x00077] in D:\cldRefactoring\cldRefactoring\Projects\1 Presentation\Picking-4.0\Picking\Picking\Helpers\Common.cs:937
--- End of managed Android.Content.ActivityNotFoundException stack trace ---
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=smb://nascld/Share/IT/Androids/Picking.apk typ=application/vnd.android.package-archive flg=0x10000000 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1816)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.ContextImpl.startActivity(ContextImpl.java:791)
at android.app.ContextImpl.startActivity(ContextImpl.java:768)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)

Thank you for your help and have a nice day

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Do you want to start another app from current app?
    Then you can refer to the following code:

    [assembly: Dependency(typeof(OpenAppAndroid))]
    namespace App14.Droid
    {
        public class OpenAppAndroid : IAppHandler
        {
            public Task<bool> LaunchApp(string packageName)
            {
    
    
                bool result = false;
    
                try
                {
    
                    PackageManager pm = Android.App.Application.Context.PackageManager;
    
                    if (IsAppInstalled(packageName))
                    {
    
                        Intent intent = pm.GetLaunchIntentForPackage(packageName);
                        if (intent != null)
                        {
    
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
    
                    else
                    {
    
                        Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
                        if (intent != null)
                        {
    
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
                }
                catch (ActivityNotFoundException)
                {
                    result = false;
                }
    
                return Task.FromResult(result);
            }
    
    
            private bool IsAppInstalled(string packageName)
            {
                PackageManager pm = Android.App.Application.Context.PackageManager;
                bool installed = false;
                try
                {
                    pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
                    installed = true;
                }
                catch (PackageManager.NameNotFoundException e)
                {
                    installed = false;
                }
    
                return installed;
            }
    
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • jb_delvauxjb_delvaux Member ✭✭

    @jezh said:
    Do you want to start another app from current app?
    Then you can refer to the following code:

    [assembly: Dependency(typeof(OpenAppAndroid))]
    namespace App14.Droid
    {
        public class OpenAppAndroid : IAppHandler
        {
            public Task<bool> LaunchApp(string packageName)
            {
    
    
                bool result = false;
    
                try
                {
    
                    PackageManager pm = Android.App.Application.Context.PackageManager;
    
                    if (IsAppInstalled(packageName))
                    {
                        
                        Intent intent = pm.GetLaunchIntentForPackage(packageName);
                        if (intent != null)
                        {
                          
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
    
                    else
                    {
                      
                        Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
                        if (intent != null)
                        {
    
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
                }
                catch (ActivityNotFoundException)
                {
                    result = false;
                }
    
                return Task.FromResult(result);
            }
    
    
            private bool IsAppInstalled(string packageName)
            {
                PackageManager pm = Android.App.Application.Context.PackageManager;
                bool installed = false;
                try
                {
                    pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
                    installed = true;
                }
                catch (PackageManager.NameNotFoundException e)
                {
                    installed = false;
                }
    
                return installed;
            }
    
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

    I may have explained myself poorly.
    My application start in picking.android. MainActivity is in picking.android. But all the pages and/or code is in the picking part of the solution. Within the picking project, I have a class called "common". That class is, as the name imply, a series of function that can be called anywhere in the application. It's also the interface with the server. Within that common, I am trying to create a function that will check what version is currently available on the server, and if that version is newer than the previous one, install it. I've managed to check for new version, and I also managed to grab the APK from the server.

    My issue is that, when I try to run the line "Android.App.Application.Context.StartActivity(PromptInstall);" I get the error specified in the original post. My supposition is that the context is NOT available in common. And I do not know how to find that context and give it to common.

    I hope I explained better. Thank you for your time and your answer ;-)

  • jezhjezh Member, Xamarin Team Xamurai

    Do you want to call the function of the picking project from your picking.android ?

    Unless the Picking Project is an Android project, you can't call it by calling method StartActivity .

    For this, you can first check the official document Sharing code on multiple platforms.

    Hope it can help you.

Sign In or Register to comment.