Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:04:59+00:00 2026-05-26T22:04:59+00:00

I have been reading the following text on the Android Developers Site, specifically under

  • 0

I have been reading the following text on the Android Developers Site, specifically under the Framework Topics -> Services -> Starting a Service.

There it states the following :

If the service does not also provide binding, the intent delivered with startService() is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.

I have a couple of questions regarding this :

  1. Does this text both apply to Services and IntentServices ?
  2. How (codewise) should this be achieved from within the Service; The service can then use the broadcast to deliver a result. and also where would the mentioned broadcast deliver the result to the original client/activity? Is there some method that should be overwritten (like onActivityResult()) or something?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:04:59+00:00Added an answer on May 26, 2026 at 10:04 pm

    Question was asked few months ago, but in case anyone is still looking for answer I hope I can help.

    In the example below we have local service, responsible for performing some time-consuming operations. Activity makes the requests to the service, but does not bind to it – just sends the intent with request. Additionally, Activity includes the information of BroadcastReceiver that should be called back when service is done with the requested task. The information is passed by PendingIntent. The service handles the task in background thread and when task is finished, service broadcasts the BroadcastReceiver with an answer.

    1. Create BroadcastReceiver subclass:

    public class DataBroadcastReceiver extends BroadcastReceiver {
       static Logger log = LoggerFactory.getLogger(DataRequestService.class);   
       @Override
       public void onReceive(Context context, Intent intent) {
          log.info(" onReceive");
       }
    }
    

    This broadcast receiver will be notified from service, when task is done.

    2. Create Service

    public class DataRequestService extends Service {
    
       private final class ServiceHandler extends Handler {
          public ServiceHandler(Looper looper) {
             super(looper);
          }
    
          @Override
          public void handleMessage(Message msg) {
             log.info("handleMessage");
             //... performing some time-consuming operation         
             Bundle bundle = msg.getData();
             PendingIntent receiver = bundle.getParcelable("receiver");
             // Perform the operation associated with PendingIntent
             try {            
                //you can attach data from the operation in the intent.
                Intent intent = new Intent();
                Bundle b = new Bundle();
                //b.putString("key", value);
                intent.putExtras(b);
                receiver.send(getApplicationContext(), status, intent);
             } catch (CanceledException e) {         
             e.printStackTrace();
             }         
          }
       }
       
       @Override
       public void onStart(Intent intent, int startId) {
          Bundle bundle = intent.getExtras();
          Message msg = mServiceHandler.obtainMessage();
          msg.setData(bundle);
          mServiceHandler.sendMessage(msg);
       }
    

    Well, the most important part is in handleMessage() method. Service simply makes the broadcasts operation for delivering results to Broadcast Receiver.

    3. You also need to register your broadcast receiver and service in Manifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ramps.servicetest"
        android:versionCode="1"
        android:versionName="1.0" >
       ....
           <service android:name=".service.DataRequestService" android:exported="false"/>
           <receiver android:name=".service.DataBroadcastReceiver"></receiver>
        </application>
    </manifest><br>
    

    4. And finally, make request to your service from Activity:

    Intent serviceIntent = new Intent(context, DataRequestService.class);   
       @Override
       public void onClick(View v) {
          //this is the intent that will be broadcasted by service.
          Intent broadcastReceiverIntent = new Intent(context, DataBroadcastReceiver.class);      
          //create pending intent for broadcasting the DataBroadcastReceiver
          PendingIntent pi = PendingIntent.getBroadcast(context, 0, broadcastReceiverIntent, 0);      
          Bundle bundle = new Bundle();            
          bundle.putParcelable("receiver", pi);
          //we want to start our service (for handling our time-consuming operation)
          Intent serviceIntent = new Intent(context, DataRequestService.class);
          serviceIntent.putExtras(bundle);
          context.startService(serviceIntent);
       }
    

    5. Delivering response to original client/activity.

    You can have abstract activity from which all your activities will be extending. This abstrct activity can automatically register/deregister itself as a response listener in broadcast receiver. Not many options here actually, but it is important that if you keep static references to your activity then you must remove the refernece when activity is destroyed.

    Regards,
    Ramps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading up on multiple PHP frameworks, especially the Zend Framework but
I have been reading the Full Text help files for CONTAINS, FREETEXT, CONTAINSTABLE and
I have been following javascript tutorial from www.w3school.com and while reading one of the
I have been reading about out of memory conditions on Linux, and the following
I have been reading through the C++ FAQ and was curious about the friend
I have been reading the MSDN documentation on subclassing and I have been successful
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading the proper article in MSDN, Strong-Named Assemblies and a related
I have been reading through the CodePlex supported open source licenses, i couldn't quite
I have been reading up on this, and it seems that if you use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.