问题描述
我正在使用 Xamarin Forms 在 Android 中开发 2 个应用程序 A 和 B &iOS 和 B 需要从 A 获取一些数据(单向通信).以下是注意事项:
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:
- 我不想从 A 打开 B(无法使用 url 方案)
- 我已将 android:sharedUserId 设置为相同
- 我尝试过 Xamarin.Auth,但它不会在应用之间共享数据
- 我试过 Xam.Plugins.Settings Nuget 插件,但它没有提供接口来传递 A 的包 ID,同时从 B 获取共享首选项
- 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
我读到可以使用相同的组 ID 在 iOS 中共享数据.除了共享 SQLite db,还有什么方法可以在 Android 中共享数据?
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?
推荐答案
是的,还有其他方法可以实现目标.
ContentProvider:只需要内容提供者如果您需要在多个应用程序之间共享数据
ContentProvider: A content provider is only required if you need to share data between multiple applications
使用内容共享构建应用程序:有一些教程,你可以参考.
Building Apps with Content Sharing: There are some tutorial which you can refer to.
我已将两者的 android:sharedUserId 设为相同
I have made android:sharedUserId same for both
如果 A 和 B 的 sharedUserId
相同,则表示 A 和 B 将运行在同一个 Sandbox
中.
If both of A and B have the same sharedUserId
, it means that A and B will run in the same Sandbox
.
所以您的 A 和 B 可以访问彼此的包资源";(如项目中的 Resources 文件夹或 Raw 文件夹)和文件(如路径:/data/data/your package name/file name
)
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
)
基于你制作的相同sharedUserId
,我建议你按照以下步骤在A和B之间共享数据.
Base on the same sharedUserId
which you have made, I suggest you follow below steps to share data between A and B.
我假设您要共享的数据是应用程序 A 中的 123"
.
I assume the data you want to share is "123"
in applicaton A.
1)在应用程序A中,将数据写入路径为/data/data/your package name/file name
的文件.该路径不需要任何权限,因为它属于内部存储:
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) 在应用程序 B 中,从文件中读取数据:
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();
}
}
}
}
注意:
以上代码基于A和B都具有相同的android:sharedUserId
.
这篇关于在 Xamarin Forms 中的应用之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!