SnapADragon
SnapADragon

Reputation: 535

Sharing data between apps in Xamarin Forms

I am using Xamarin Forms to develop 2 applications A and B in Android & iOS, and B would need to fetch some data from A (1 way communication). Following are the points to note:

I read that it is possible to share data in iOS using same Group Id. Is there any way to share data in Android apart from sharing SQLite db?

Upvotes: 1

Views: 4114

Answers (1)

Robbit
Robbit

Reputation: 4358

Yes, there are other ways to achieve the goal.


I have made android:sharedUserId same for both

If both of A and B have the same sharedUserId, it means that A and B will run in the same Sandbox.

So your A and B can access each other's "Package Resources" (like Resources folder or Raw folder in your project) and files (like the paths:/data/data/your package name/file name)


Base on the same sharedUserId which you have made, I suggest you follow below steps to share data between A and B.

I assume the data you want to share is "123" in applicaton A.

  1) In application A, write the data to the file which path is /data/data/your package name/file name. This path doesn't need any permission, because it belongs to internal storage:

namespace ShareA
{
    [Activity(Label = "ShareA", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            WriteSettings(this, "123");
        }

        private void WriteSettings(MainActivity context, string data)
        {
            try
            {
                using (var writer = new StreamWriter(
              OpenFileOutput("settings.dat", Android.Content.FileCreationMode.Private)))
                    writer.Write(data);

                Toast.MakeText(context, "Settings saved", ToastLength.Short).Show();
            }
            catch (Exception e)
            {
                Android.Util.Log.Error("lv",e.Message);
                Toast.MakeText(context, "Settings not saved", ToastLength.Short).Show();
            }

        }
    }
}

  2) In application B, read the data from the file:

namespace ShareB
{
    [Activity(Label = "ShareB", MainLauncher = true)]
    public class MainActivity : Activity
    {
        TextView textView;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            textView = this.FindViewById<TextView>(Resource.Id.textView1);

            try
            {
                //get A's context
                Context ctx = this.CreatePackageContext(
                        "ShareA.ShareA", Android.Content.PackageContextFlags.IgnoreSecurity);
                //Read data
                string msg = ReadSettings(ctx);
                textView.Text= msg;
                Toast.MakeText(this, "DealFile2 Settings read" + msg, ToastLength.Short).Show();
                //Or you can change the data from B
                WriteSettings(ctx, "deal file2 write");
            }
            catch (PackageManager.NameNotFoundException e)
            {
                // TODO Auto-generated catch block
                Android.Util.Log.Error("lv", e.Message);
            }
        }

        private string ReadSettings(Context context)
        {
            try
            {   // context is from A
                using (var reader = new StreamReader(context.OpenFileInput("settings.dat")))
                {
                    return reader.ReadToEnd();
                }
            }
            catch
            {
                return "";
            }
        }

        private void WriteSettings(Context context, string data)
        {
            try
            {   // context is from A
                using (var writer = new StreamWriter(
              context.OpenFileOutput("settings.dat", Android.Content.FileCreationMode.Private)))
                    writer.Write(data);

                Toast.MakeText(context, "Settings saved", ToastLength.Short).Show();
            }
            catch (Exception e)
            {
                Android.Util.Log.Error("lv", e.Message);
                Toast.MakeText(context, "Settings not saved", ToastLength.Short).Show();
            }
        }
    }
}

Note:

The above code is based on both A and B have the same android:sharedUserId.

Upvotes: 1

Related Questions