Xamarin.Forms How do I get Intent to work?

34 Views Asked by At

I have a Xamarin.Forms solution and am finding it difficult to implement the Intent class as this library is part of Android.Content.

My solution has 2 projects. Project1 & Project1.Android.

I cannot seem to use the following in my Project1 project:

using Android.Content; using Android.App;

Am I missing something here? Any help is greatly appreciated.

I need to use Intent to broadcast a message to another app on the Android device.

using Android.Content; using Android.App;

1

There are 1 best solutions below

0
Jessie Zhang -MSFT On

Since your app is a Xamarin.Form app, you cannot invoke code from android platform directly. But you can use DependencyService to invoke android platform code from shared code. The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.

From official document Xamarin.Forms DependencyService, we could know that the process for using the DependencyService to invoke native platform functionality is to:

1.Create an interface for the native platform functionality, in shared code. For more information, see Create an interface.

2.Implement the interface in the required platform projects. For more information, see Implement the interface on each platform.

3.Register the platform implementations with the DependencyService. This enables Xamarin.Forms to locate the platform implementations at runtime. For more information, see Register the platform implementations.

4.Resolve the platform implementations from shared code, and invoke them. For more information, see Resolve the platform implementations.

For more information, please check document: Xamarin.Forms DependencyService.

And there is an official sample about this, you can check it here: Dependency Service