Search code examples
androidandroid-studioandroid-intentandroid-activityservice

How can I send data from a Service to an Activity in Android Studio?


I have a service that is started in Activity1. Activity1 sends a String which is received by Service1. When the recent items are closed and Service1's Notification is clicked I want Service1 to open Activity1 using a String from Service1.

Your assistance will be greatly appreciated.

TIA

TheRealMrP


Solution

  • Just Refer this

    Using Binding/Unbinding of the service

    https://medium.com/@sahityakumarsuman/android-service-communication-with-activity-2c01e537ab03

    OR

    Using interface trick.

    You can create a interface, define the listener method inside the activity and call the listener method inside your service.

    WARNING: It can give you null pointer error, so don't forget to check if the service and the activity are alive while calling.