Android Question send , receive string between two applications

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

How Can I send and receive string between my application and other application ?
I want to open the other app. to read data from a card, then send a number to the main app.
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
you can share a string with an intent if you have a sender and a receiver. the 2 apps must be set up accordingly. do you know how to share things? both apps are yours, yes? another option might be to turn 1 of the apps into a multi-activity app (with the 2nd app as an activity). you would simply decide which app is the "main" app.

i realize you might simply prefer to have 2 apps. no problem, but you will have to modify each: the reader becomes the sender, and the main app becomes the receiver of the shared number. it's no big deal to set up. follow?

another option, if you can't handle sharing, would be for the reader to store the number in a file and then the main app could read from that file. both apps have to know where that file is (dir.external), and you have to make sure you run the app that needs the number after you run the app that reads it, and that the file is deleted after it's been read (otherwise, the app that reads the number could be reading some number from the past).

i vote for the multi-activity app. all kinds of ways to share the number without getting into intents.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thanks @drgottjr
I have two apps and both of them are mine. By searching I found that there is a way in Java to send data between two applications:
B4X:
Intent.PutExtra   'to send string'
Intent.GetExtra    'to receive string'
I don't know, is there a corresponding code in the B4A?
can it be used in B4a code?
If this is not possible, I have two options:
- One of the two applications writes a text file and the other reads it (As you say).
- To put the value in the clipboard..
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
intent.putextra, input.getextra šŸ‘ that's it! b4a.
the text file approach can work, but there's more housekeeping that you'd be responsible for.
i forgot about the clipboard. i do use it between activities (in a multi-activity app). i'm guessing there's no issue communicating between apps.

see how far you get with putextra and getextra. then come back if you're in a bind. what you need to do is explained if you search some.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I tried to modify this code, but not working:
B4X:
Dim in As Intent
    in.Initialize(in.ACTION_MAIN, "")   ' I added this line, originally not found
   
Dim pm As PackageManager
in = pm.GetApplicationIntent("b4a.TelegramBot")
in.PutExtra("android.intent.extra.BotMessage","String I want on second app")
If in.IsInitialized Then StartActivity(in)

the error is:
java.lang.RuntimeException: Object should first be initialized (Intent).
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
not clear where the error is occurring. more of the log is needed..
i want to look into your choice of code a little bit. some things don't look right, but there are a lot of ways to do things. are you cutting and pasting from different posts?

try this to keep things simpler for the moment:
B4X:
    dim pm as packemanager
    dim intent as intent
    intent.Initialize(intent.ACTION_SEND,"")
    intent.PutExtra("text","hello, kattah!" )
    intent.SetType("text/plain")

    Dim possibleApps As List = pm.QueryIntentActivities(intent)
                            
    If possibleApps.IsInitialized Then
        Log("your device claims it has " & possibleApps.Size & " app(s) to handle images...")
        StartActivity(intent)
    End If

in my case, i usually prefer to choose which app i want to share with. in your case, it's different because you're using a dedicated receiver, but let's start simple.
as you may know, you have to make sure there is an app that can handle your share, otherwise there is a crash. that's what pm.queryintentactivities() is about. there is another shorter way to handle that.
so, try the above. if you still get the same error message, my guess is you might have dimmed another intent somewhere else in your code. the error log would probably clarify that.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
instead of this:
B4X:
    Dim in As Intent
    in.Initialize(in.ACTION_MAIN, "")   ' I added this line, originally not found
  
   Dim pm As PackageManager
   in = pm.GetApplicationIntent("b4a.TelegramBot")
   in.PutExtra("android.intent.extra.BotMessage","String I want on second app")
   If in.IsInitialized Then StartActivity(in)

just do this (to start):
B4X:
Dim in As Intent
Dim pm As PackageManager
in = pm.GetApplicationIntent("YOUR_FULL_PACKAGE_NAME")
If in.IsInitialized Then 
   StartActivity(in)
else
   log("intent was not initialixzed")
end if

are you sure "b4a.TelegramBot" is the full package name of your second app? if so, then this simpler code will start the second app. no sharing for the moment; let's get an intent going
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the error is:
java.lang.RuntimeException: Object should first be initialized (Intent).
the error is probably related to a not existent answer from
in = pm.GetApplicationIntent("b4a.TelegramBot")
if PM does not find an installed app with this packagename it probably return null or an uninitialized Intent.

What is the exact packagename of the telegrambot app ? It must be an installed app on the android Device for sure.
Does the telegrambot work in an b4a app? My telegrambot library is a b4j library and the bot is expected to run in a server app.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
another simple way:
B4X:
    intent.Initialize(intent.ACTION_SEND,"")

    intent.SetComponent("FULL_PACKAGE_NAME")
    intent.PutExtra("text","hello, kattah!" )
    intent.SetType("text/plain")

note: as donmanfred just mentioned, the error is from pm.getpackageintent. intent.setcomponent() will give you the same error. there is either no package so named, or the package name is incorrect. this is why i ask if "b4a.TelegramBot" is the full package name (and is it deployed to the device yet?). to be sure, you need to test for intent.isinitialized right after pm.getpackageinter() or intent.setcomponent(). you have the test too late. follow?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if your receiver doesn't exist yet, then you don't want to refer to it specifically. this will cause a crash if you launch the intent without a valid app to handle it.
if you just declare an intent for action_send and put your extra and set your intent type, then all you have to do is check to make sure there is an app available to handle the intent. if the packagemanager returns a list of apps, then you're good to go. you'll see the list and you select one. depending on the app you choose, you should see the text you wanted to share.

ultimately, when you get your receiver going, it will see the text (presumably the number you wanted to send it) and act accordingly.
if the receiver isn't operational yet, just do a general action_send type = text/plain, and the os will find an existing app for you. ok?
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
If this is not possible, I have two options:
- One of the two applications writes a text file and the other reads it (As you say).
- To put the value in the clipboard..
A traditional way that works across different OSes and technologies is good old fashioned TCP sockets. You can transfer data both ways, you have a lot of the infrastructure details handled for you.
Now that I think of it, I do not know if Android actually would let you do that... I know it works well on the PC.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thanks @drgottjr, @DonManfred ,@Didier9
This is Provider and Requester application.
I started running the Requester application until the package (b4a.Requester) was present, then I ran the Provider to send the string, then the Requester application again to receive the string.
There are no error messages, but the Requester app has not received any text.
 

Attachments

  • Provider.zip
    339.3 KB · Views: 174
  • Requester.zip
    347.5 KB · Views: 182
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is Provider and Requester application.
1. Where did you copy the code from?
2. Why you DID NOT copy any needed Manifestsettings?

I guess you are using this Tutorial.

Go again over the tutorial, understand it, and adapt your Manifest.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thanks for help @DonManfred and Sorry for the inconvenience . I will try to follow and understand the tutorial.
 
Last edited:
Upvote 0
Top