Android Question Sharing Info between 3 apps

Setlodi

Member
Hi there

I hope you experts can help. I have 3 applications, App A, App B and App C. The main function of App A is collect user information, e.g. Name, Surname, Telephone. Now I want App B and App C to access this information. I cannot have 1 app doing all that as both 3 app are very big and complex. I want when the user opens App B or App C to already see Name, Surname, Telephone collected by App A.

I have used FileProvider to create a Share Folder in App A but I don't know how to access this folder from App B and App C. I can save information in KeyValueStore/SQLite db/Text File. All I want is to be able to access this file from App B and App C.

I'm stuck here...
 
Solution
Sorted.

1) I changed MIME type in Manifest of Open KVS to <data android:mimeType="*/*" />

So this allows me to
1) Enter Name and Surname in Share KVS app

B4X:
kvs.Initialize(xui.DefaultFolder, "testingKVS.dat")
Name = txtName.Text
Surname = txtSurname.Text
kvs.Put("Name", Name)
kvs.Put("Surname", Surname)

2) Copy testingKVS.dat to Shared Folder
B4X:
File.Copy(File.DirInternal, "testingKVS.dat", Starter.shared, "testingKVS.dat")

And now in Open KVS app
1) Copy testingKVS.dat into DirInternal using URI
B4X:
File.Copy("ContentDir", uri,  File.DirInternal, "testingKVS.dat")

2)Initialise and Get KVS info and show it
B4X:
kvs.Initialize(File.DirInternal, "testingKVS.dat")
lblName.Text = kvs.Get("Name")
lblSurname.Text =...

Brian Dean

Well-Known Member
Licensed User
Longtime User
Although you have described your requirements fairly well there is more information that would be useful to have. For instance -

- How many users do you expect - hundreds, thousands, hundreds of thousands? Or maybe fewer that fifty.

- Will this app be on the PlayStore or will you distribute it in some other way?

- Do you already have any form of internet storage or server space?

When I started this reply I had intended to suggest a few ideas but now, without more information, I think that would probably be a waste of our time.
 
Upvote 0

Setlodi

Member
Hi Brian, thanks for your response. Herewith the answers:

- How many users do you expect - hundreds, thousands, hundreds of thousands? Or maybe fewer that fifty.
Maximum 1000 users. This is a closed user group.

- Will this app be on the PlayStore or will you distribute it in some other way?
No this will not be on any store. The apps will run on android devices and will be installed on the device in the office before being given to the client

- Do you already have any form of internet storage or server space?
I have a VPS

All three apps will be running on 1 single device and information will be related to the device.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check:
 
Upvote 0

Setlodi

Member
Check:

Thanks aeric. How would this work if my apps are running on the same device?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
All three apps will be running on 1 single device and information will be related to the device.
How would this work if my apps are running on the same device?
My solution is not suitable in this case.
You may need to look at accessing external storage.

 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I am not answering this as an expert - I am just trying to help and maybe stimulate some better ideas. Here are some thoughts . . .

1. Simply write your app for a downlevel sdk. If you have a reason not to do this then make sure that it is a real one.

2. If you cannot adopt solution 1 then look at the @agraham solution mentioned by @aeric.

All of the remaining ideas require internet connections to pass data between apps, and they have downsides . . .

3. (This is the only option that I have used myself - at the family and friends level.) Use FTP to pass data between the apps. FTP is unfashionable but it is very easy to use. It is difficult these days to find a free provider, and although I have been using the same one for over twenty years I could not recommend them now.

4. Use some other form of shared cloud storage, such as GoogleDrive. Much more messy than FTP and might not be a viable path.

5. If the task is to pass a one-time small amount of data between two apps is there a Firebase Messaging solution? I have no experience of this, but it might be possible. It is also a free solution, if it exists.

6. As we are moving towards off-the-wall solutions, I wonder if there is an MQTT path. MQTT is incredibly easy to use and there are free providers. I am looking for an excuse to try it out myself.

That is all I have. Maybe it will encourage better answers from others who have more experience. I will just repeat that you need to be sure that you have a good reason not to use option 1.

Edit : As soon as I posted this I realised that most of these paths would require some way of identifying the user device, another perennial problem posted on this forum.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my example of using ManageExternalStorage class.

 
Upvote 0

Setlodi

Member
Ok, this is what I'm trying. I have 2 apps, Share KVS and Open KVS. Both running in the same device. I'm initializing my KVS as follows:
Initializing KVS:
kvs.Initialize(xui.DefaultFolder, "testingKVS.dat")

Now in my Share KVS app I have the following code:


Share KVS:
Sub Globals
    Public IME As IME
    Public xui As XUI
    Public kvs As KeyValueStore
    Private txtName As BRB4XFloatTextField
    Private txtSurname As BRB4XFloatTextField
    Private lblFullName As Label
    Public Name As String
    Public Surname As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Mainpage")
    kvs.Initialize(xui.DefaultFolder, "testingKVS.dat")
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

Public Sub Save_to_KVS
    IME.HideKeyboard
'    kvs.DeleteAll
    Name = txtName.Text
    Surname = txtSurname.Text
    kvs.Put("Name", Name)
    kvs.Put("Surname", Surname)
    Log("Original Name = " & Name)
    Log("Original Surname = " & Surname)
    lblFullName.Text = Name &" "& Surname
End Sub


Private Sub btnSave_Click
    Dim FilesList As List
    FilesList = File.ListFiles(Starter.shared)
    Log("FileList = "  & FilesList)
    Save_to_KVS
    'copy the file to the shared folder
'    File.Copy(File.DirAssets, "b4a.png", Starter.shared, "b4a.png")
    File.Copy(File.DirInternal, "testingKVS.dat", Starter.shared, "testingKVS.dat")
    Dim in As Intent
'    in.Initialize(in.ACTION_SEND, CreateFileProviderUri(Starter.shared, "b4a.png"))
    in.Initialize(in.ACTION_SEND, CreateFileProviderUri(Starter.shared, "testingKVS.dat"))
    in.Flags = 1
    StartActivity(in)
End Sub

And the code for Open KVS :

Open KVS:
Sub Globals
Public kvs As KeyValueStore
    Private lblName As Label
    Private lblSurname As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Mainpage")
End Sub

Sub Activity_Resume
    If IsRelevantIntent(Activity.GetStartingIntent) Then
        Dim starting As Intent = Activity.GetStartingIntent
        If starting.GetData.StartsWith("content") Then
            Dim starting As Intent = Activity.GetStartingIntent
            Dim uri As String = starting.GetData
            Log("*** URI = " & uri)
            kvs.Initialize(uri, "testingKVS.dat")
            Log("kvsName = " &kvs.Get("Name"))
            Log("kvsSurname = " &kvs.Get("Surname"))
            lblName.Text = kvs.Get("Name")
            lblSurname.Text = kvs.Get("Surname")
        Else
            Dim in As JavaObject = Activity.GetStartingIntent
            Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
        End If
    End If
End Sub

Private Sub IsRelevantIntent(in As Intent) As Boolean
    If in.IsInitialized And in <> OldIntent And in.Action = in.ACTION_SEND Then
        OldIntent = in
        Return True
    End If
    Return False
End Sub

No the following 2 scenatios happen when I run the apps:

1) when I run Share KVS with the following codeit gives me the option to open with Open KVS

Running Share KVS incluging png file:
File.Copy(File.DirAssets, "b4a.png", Starter.shared, "b4a.png")
    Dim in As Intent
    in.Initialize(in.ACTION_SEND, CreateFileProviderUri(Starter.shared, "b4a.png"))
    in.Flags = 1
    StartActivity(in)

Screenshot_20240601-114258.png


2) When I run Share KVS app with the following codeit gives me the NO option to open with Open KVS

Running Share KVS app including KVS dat file:
'    File.Copy(File.DirInternal, "testingKVS.dat", Starter.shared, "testingKVS.dat")
    Dim in As Intent
    in.Initialize(in.ACTION_SEND, CreateFileProviderUri(Starter.shared, "testingKVS.dat"))
    in.Flags = 1
    StartActivity(in)

Screenshot_20240601-114115.png


I'm wondering why I'm able to have an option to open with Open KVS app when I'm using a png file but when I use the KVS dat file it's giving me no option. The only things I'm changing is the files (testingKVS.dat and b4a.png), otherwise every thing stays the same.
 
Upvote 0

Setlodi

Member
Sorted.

1) I changed MIME type in Manifest of Open KVS to <data android:mimeType="*/*" />

So this allows me to
1) Enter Name and Surname in Share KVS app

B4X:
kvs.Initialize(xui.DefaultFolder, "testingKVS.dat")
Name = txtName.Text
Surname = txtSurname.Text
kvs.Put("Name", Name)
kvs.Put("Surname", Surname)

2) Copy testingKVS.dat to Shared Folder
B4X:
File.Copy(File.DirInternal, "testingKVS.dat", Starter.shared, "testingKVS.dat")

And now in Open KVS app
1) Copy testingKVS.dat into DirInternal using URI
B4X:
File.Copy("ContentDir", uri,  File.DirInternal, "testingKVS.dat")

2)Initialise and Get KVS info and show it
B4X:
kvs.Initialize(File.DirInternal, "testingKVS.dat")
lblName.Text = kvs.Get("Name")
lblSurname.Text = kvs.Get("Surname")
 
Upvote 0
Solution

aeric

Expert
Licensed User
Longtime User
I haven't checked this example but if you want to use intent then check this:
 
Upvote 0
Top