Android: How to change textview of another application - android

I have to different applications. First application has background service. Second application has activity with textview.
Question: how can I detect second app launch using service from first app? And how to change textview's text from service?
Thanks.

You can't detect the launch on an other application, neither you can change the TextView of a different application directly.
But you can achieve this with cooperation. The second application could notify the Service about being started, and the Service could ask the First Activity to change its TextView.
This can be achieved with intents. The Activity in the first application could start the Service as it starts, and the Service can send a broadcast or send an intent to the activity directly to request it to modify its TextView.
If you want to notify the Activity only when it runs, and don't want to wake it up like in the previous example, then I suggest you to read this link about bound services. It gives you a complete example on how to bind to a service, and also describes how to make requests back to the Activity.
Notice that this example does not show how the service can respond to
the client. If you want the service to respond, then you need to also
create a Messenger in the client. Then when the client receives the
onServiceConnected() callback, it sends a Message to the service that
includes the client's Messenger in the replyTo parameter of the send()
method.

use broadcastreceiver in the that update your another application.
if you are using service then it will not update your GUI of another application.
broadcastreceiver is the best practice to do which you want.

Related

android studio it's good to use bind service on background service?

I am creating an chat app which is connected to web socket, using a background service. But I should connect it to the activity to show the new messages. Maybe in the main conversations list, or inside the conversation itself.
So, first, I want to check if my service is running, then getting it bound to the opened activity.
And I want the service to tell the activities that there is a new message.
So, is it good to do it using 'bindService'? Because I feel that there's something not right.
You can send messages from the Service to Activities with the BroadcastReceiver class. I would get your information from the following link:
https://developer.android.com/guide/components/broadcasts

Android Service Activity Interaction using AIDL

I am trying to make 2 applications which will interact with each other using AIDL.
Application 1: Will be a service.
Application 2: Will be Activity(with a button) which will show some data which will be fetched by Application 1 service.
Now to start this interaction I know we can make one AIDL file in both applications and when user presses the button in application 2 we can involve the function of service from application one. That is lets say application 2 requests the current time then application 1(Service) will fetch the current time and return it to application 2.
My doubt is that I want to interact the other way round. I want to inform the activity from service when some particular digit occurs in time(or some other event). I am not sure how to proceed with this way of communication ie from Service to Activity.
Some pointers will be really helpful.
You know you can send message from Activity to service, Reference Bound Service
By Following above tutorial You should Consider Sending Handler from Activity to Service using Messenger Class in Intent.
So now Service and Activity can send message to each other which will execute corresponding Handlers
Use Broadcast receiver in Activity and let Service broadcast messages (with same Intent as used by Broadcast receiver in Activity). These messages which are broadcasted from Service will be received by broadcast receiver in the Activity.

Advice on interactions between service, activity, and notifications

I am implementing a Service which will start when MainActivity starts. This service checks for and maintains a network connection.
I have not bound the Service to the Activity because I want the service to continue running even if the activity isn't available. The Service will receive messages from the server that it is connected to.
I am struggling to choose the best logic to do the following when the service receives a message.
Check if MainActivity is currently open and in front of the user
If it is call some methods in the activity to interact with the UI
If there is no activity update the notification are.
My question is;
How do I correctly check if the activity is running in the UI from my service? I know that I could bind the service but I wouldn't want to unbind it if the activity is closed. Would that be a problem?
I will somehow need to send messages from the service to the activity. Given the above scenario what would be the best way to do this?
Do it differently.
If your Service does not run in a separate process from your Activities, then your Service can provide a synchronized (multithread-safe) list of messages via a subcalssed Application object, where your Activity can look it up. However, this would only be best if the polling occurs on certain other events.
If you want to sort of "push" the message to your Activity, your Activity should register with your service upon finding out that it runs, not the other way round. In this scenario, your Activity should implement an interface through which the Service can notify your Activity of new messages in a Thread-safe way.
Of course you could also go straightforward and simply post notifications which open an Activity, but as I understood it, you want to provide a more smooth integration.
An idea would be to let your Service send status bar notifications when a new update is available.
Implement the notification such that when clicked, to open MainActivity.
Then, when the user will click on the notification, if the MainActivity is already running then it will be brought to front, otherwise it will be started.
So this basically eliminates the need to check if MainActivity is currently open in front of the user (which I see as a bad practice).

Start activity/send intent from object instance within service

I have the following situation: A service is running in the background of my application and regularly receives UDP packets. It uses an instance of my HandleMessageAgent class which analyses every message and shall start a new activity if necessary.
I would like to perform the following task: No matter which activity is in the front (as long as the service is running) I would like to inform the user about an incoming message under certain circumstances. I also need to update the information regularly as long as it is valid. Afterwards it should be closed automatically.
At first I thought about using a Dialog, but I think I cannot use it when the activity is not visible. Therefore I decided to use an activity, as it can be started from a service all the time.
I want to start the activity within the HandleMessageAgent object (in a method). My problem is, that I do not know how I can define an Intent to start an activity within an object, as the Context is not clear to me.
Is there a more elegant way to perform this task? Or can anybody help me with starting an activity from an object method within a service? Thank you!
There are two situations to consider:
When your service need to notify user your activities are not active, because some other app is active. In this case you should notify user via a system-preferred way: Android Notifications. You should not forcefully show dialogs or activities if user is using some other app. That's what notifications are for.
If one of your activities is active (no matter which) then your service should send a broadcast and interested activities should listen for it and act upon it. That way your service will not depend upon specific activity and will not need to keep track which activities are active at the moment the notification must be shown.
You can make your object Parcelable and add it to the Intent that will start the Activity.
Or you can put it in a subclass of Application because that instance is shared between your activities and your services (as long as they're in the same process)
You might need the "START_NEW_TASK" flag on the Intent

Android: How to communicate between 2 activities

I have a problem:
I have a UI thread which displays webwiew and another chatActivity which displays chat.
I keep on getting data from server which would be displayed on both Activities.
What is the best way to do this viz doInBackground or Service,
If service, than can i bind 2 activity with 1 service i.e. if user press logout from UI or chatActivity, both activity and service should die otherwise service sud update both activity contents.
I am new to 'service' so any reference or sample code would be helpful.
Sounds like you need to broadcast some information. You than will be able to set broadcast receivers in any activity/service you would like to get notified.
Read more online about Broadcastreceiver and about send broadcast

Categories

Resources