受欢迎的博客标签

Android|Visual Studio|Xamarin.Forms|How to share data from one app to another using Xamarin

Published

This tutorial runs through the basic process of creating a share button, implementing the share Intent, passing your content, and building the chooser list.

Implement a Share Intent

Step 1: Start a New Android Project

 

Setting up your Xamarin.Android application as a sharing target

Step 2:Create a Send Intent

Add an intent filter to the AndroidManifest.xml file like below.

path:\AndroidAppFormAsShareTarget.Android\Properties\AndroidManifest.xml

<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>
<?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.androidappformassharetarget">
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
  <application android:label="AndroidAppFormAsShareTarget.Android" android:theme="@style/MainTheme">
  +  <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 3:Set the Sharing Type

Set a MIME type for the content you're shared.

This will determine which applications the chooser list presents to your users. Plain text, HTML, images and videos are among the common types to share. The following Java code demonstrates sending plain text:

sharingIntent.setType("text/plain");
This is a flexible option, as you can send plain text reliably through many different

88

 

Set the name attribute on the activity that will receive the callback when a user clicks the share icon.

 

Add code

 
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            if (Intent.ActionSend.Equals(Intent.Action) && Intent.Type != null && "text/plain".Equals(Intent.Type))
            {
              //  handleSendUrl();
            }

new

namespace AndroidAppFormAsShareTarget.Droid
{
    [Activity(Label = "AndroidAppFormAsShareTarget", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());


            if (Intent.ActionSend.Equals(Intent.Action) && Intent.Type != null && "text/plain".Equals(Intent.Type))
            {
              //  handleSendUrl();
            }
        }
...
}

 

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Xamarin.Essentials plugin provides 20+ cross-platform APIs for mobile application development. Xamarin.Essentials API works with all Xamarin.Forms, Xamarin.Android, Xamarin.iOS, or UWP application that can be accessed from shared code. We are developing Xamarin with Android, iOS and UWP apps but now Xamarin.Essentials overcomes the problem, and developers can access every native platform API using C#. This plugin provides many APIs so initially, there is no need for more plugins for Xamarin. Xamarin.Essentials plugin impacts your app's minimum size.

 

https://www.c-sharpcorner.com/article/xamarin-forms-share-data-using-xamarin-essentials/(Share to other app with Xamarin Essentials)

https://docs.microsoft.com/en-us/xamarin/essentials/?context=xamarin/android

Xamarin.Essentials release notes:https://docs.microsoft.com/en-us/xamarin/essentials/release-notes/

Setting up your Xamarin.Forms application as a sharing target

https://criticalhittech.com/2021/03/25/setting-up-your-xamarin-forms-application-as-a-sharing-target/

 

Share.Plugin In Xamarin.Forms Application For Android And UWP

https://www.c-sharpcorner.com/article/share-plugin-in-xamarin-forms-application-for-android-and-uwp/