1

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 don't want to open B from A (No url scheme possible)
  • I have made android:sharedUserId same for both
  • I tried Xamarin.Auth, but it doesn't share data between apps
  • I tried Xam.Plugins.Settings Nuget plugin, but it doesn't provide interface to pass package Id of A while fetching the Shared Preferences from B

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?

SnapADragon
  • 536
  • 7
  • 21
  • Did app A and app B consume the same api and server, you could send all the data you need from B to the server from A and then consume that data from the server in B. – FabriBertani Jan 10 '18 at 18:22
  • You are planning to use Internet, Bluetooth or infrared for the communication for two phones? Or there are two apps that passing information within a phone? – Json Jan 11 '18 at 00:04
  • @FabriBertani Apps can't communicate with each other's Backend Server. – SnapADragon Jan 12 '18 at 11:48
  • @Janson I wish to share data between 2 apps within a single phone, Pardon if it was not clear in the post. – SnapADragon Jan 12 '18 at 11:50
  • Have you solved your question? Please leave me a message if you have seen this, thanks!!!! – Robbit Jan 17 '18 at 05:33

1 Answers1

1

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.

Community
  • 1
  • 1
Robbit
  • 4,300
  • 1
  • 13
  • 29
  • 1
    Thanks for the detailed answer, but I was looking for something more specific to Xamarin. And, I didn't mention it earlier but I didn't want to share data using files too - My Bad :( – SnapADragon Jan 12 '18 at 12:04
  • I was wondering if it was possible to share data using SharedPreferences somehow in Xamarin. Not able to find the exact classes for it in Xamarin, and Xam.Plugins.Settings uses SharedPreferences for use within single app only. – SnapADragon Jan 12 '18 at 12:14
  • You want use SharedPreferences? You can use the same way to achieve it. Create a SP file in A, and get it in B. – Robbit Jan 12 '18 at 12:50
  • Apologies for the late response - Yes I tried your code and it is working. Thanks a lot. – SnapADragon Jan 17 '18 at 09:35
  • So, you are using SharedPreferences in your project not Files? – Robbit Jan 17 '18 at 09:36
  • I have no idea how to use SharedPreferences in Files. I used the same method suggested in your code. I wanted to use something else to share data other than using filesystem, but this will do too. Thanks. – SnapADragon Jan 17 '18 at 09:39