How to communicate with the service on Android

Asked 2 years ago, Updated 2 years ago, 70 views


If the service started with startService assumes that it runs in the same process as Activity,

Data interaction between services and Activity is done without complicated methods such as binding. Can I use the Android.app.Application instance as a global variable?

In other words, if the service is running, does it always have a base application instance?

I don't understand the general manners of Android, so I may be saying something wrong, but
It would be very helpful if you could reply.
Thank you for your cooperation.

android

2022-09-30 20:59

1 Answers

If the application component starts and the application does not have any other running components, the Android system starts a new Linux process for the application with a single thread to run.

Processes and Threads

Android Components,

  • Activity
  • BroadcastReceiver
  • ContentProvider
  • Service

When one of the works, the Android system starts the application process.The onCreate() of the android.app.Application instance is invoked.

Therefore,

If the service is running, is there always an application instance underlying it?

The answer to this question is "must exist."

However, since the process is killed by the OS, I think Application should be built as a short-lived datastore with a thread-safe design.You can also read the same data, but not communicate.

You can have Intent have data in your Intent while you can have data in your Service.

Conversely, Activity cannot determine when the Service process is finished (it is better to pass data in BroadcastReceiver), and as mentioned above, Application process is missing OS/code>.


2022-09-30 20:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.