Setting up your Xamarin.Android application as a sharing target.
Xamarin Android App Respond to 'Share To' Request.
Basic
1.By default, your activity will not show up in Android's application launcher screen. This is because there will likely be many activities in your application, and you don't want an icon for every one.
2.You have to register your app for share intent. Once done the OS will list your app in the list when you click the share icon.
3.If Android has more than one possible match for an implicit intent, then it may ask the user to select the component to handle the action:
Create a Xamarin.Android application project with Visual Studio
see:
https://docs.microsoft.com/en-us/xamarin.android/
register your app for share intent - registered in the AndroidManifest.xml
Adding Intent Filters to Xamarin.Android in AndroidManifest.xml file,
Xamarin.Android will automatically register the Intent Filters in the manifest at build time with the necessary XML attribute.
or
register your app for share intent - Add the code for Intent to your activity in which you want to receive the image
Testing App-Links
step 1:Adding Intent Filters to Xamarin.Android
Add an intent filter to the AndroidManifest.xml file.
one intent-filter with the android:mimetype set to “text/plain” for sharing links.
F:\stock\ReadDZHRealTime\src\WinForm\AndroidApp\Properties\AndroidManifest.xml
befor
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.androidapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="30" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
// add here
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
new
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.androidapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="30" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Step 2:In OnCreate method check the intent
F:\stock\ReadDZHRealTime\src\WinForm\AndroidApp\MainActivity.cs
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
+if (Intent.ActionSend.Equals(Intent.Action) &&
+ Intent.Type != null &&
+ "text/plain".Equals(Intent.Type))
+ {
+ // handleSendUrl();
+ }
+}
In the MainActivity.OnCreate method, first check if the Intent.Action and Intent.Type values corresponds to the values set in the AndroidManifest. If they match that means an application or browser has requested to share a url link to our application and now we can display the UI for handling this. In the sample below, a Dialog is displayed with the url and a text input for the user to enter a description for the url.
Other
You can add multiple “intent-filter” tags for each media type that you want to allow sharing to your application as shown
For this example, we’ve set up one intent-filter with the android:mimetype set to “text/plain” for sharing links.
step 1:Launchable from Application Chooser
To specify which one should be launchable from the application launcher, use the MainLauncher property.
IntentFilterAttribute
Namespace:Android.App
Assembly:Mono.Android.dll
method 2:
You have to register your app for share intent. Once done the OS will list your app in the list when you click the share icon
step 1:Add the below to your activity in which you want to receive the image
[IntentFilter(new[]{Intent.ActionSend},Categories = new[]{Intent.CategoryDefault},DataMimeType = "image/*",Label = "Your application name")]
eg:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AndroidApp
{
[Activity(Label = "YourActivity")]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "image/*", Label = "activity label")]
public class YourActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
}
}
}
Step 2:In OnCreate method check the intent
if (Intent.Action == Intent.ActionSend)
{
if(Intent.Extras.ContainsKey(Intent.ExtraStream)){
Uri fileUrl =(Android.Net.Uri)Intent.Extras.GetParcelable(Intent.ExtraStream));
}
}
eg:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AndroidApp
{
[Activity(Label = "YourActivity")]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "image/*", Label = "activity label")]
public class YourActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
if (Intent.Action == Intent.ActionSend)
{
if (Intent.Extras.ContainsKey(Intent.ExtraStream))
{
Uri fileUrl = (Android.Net.Uri)Intent.Extras.GetParcelable(Intent.ExtraStream));
}
}
}
}
}