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 8678929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:50:46+00:00 2026-06-12T20:50:46+00:00

I have an activity and an intentService in the same application. The service must

  • 0

I have an activity and an intentService in the same application. The service must keep running after the activity ends so I do not want to bind. I have been googling for hours and can’t find a single good example of how to do this. I’m able to start the service and pass extras to it but now the service has to use Messenger to send data back to the activity.

I read that this process basically involves…
Calling Message.obtain() to get an empty Message object
Populating that object with whatever data is needed
Calling send() on the Messenger, supplying the message as a parameter

But I can’t find any code examples on how to do this.

several posts refer to a messengerService example in the SDK sample APIDemos, which I have, but I can’t find anything there.
thanks, Gary

  • 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-06-12T20:50:48+00:00Added an answer on June 12, 2026 at 8:50 pm

    For the record I’ll answer my own question as it might be useful to others…
    (I’m using a regular Service, not an IntentService as it needs to stay active)

    For the activity to receive messages from the service, it has to instantiate a Handler as so…

       private Handler handler = new Handler() 
    {
        public void handleMessage(Message message) 
        {
            Object path = message.obj;
    
            if (message.arg1 == 5 && path != null)
            {
                String myString = (String) message.obj;
                Gson gson = new Gson();
                MapPlot mapleg = gson.fromJson(myString, MapPlot.class);
                String astr = "debug";
                astr = astr + " ";
            }
        };
    };
    

    The above code consists of my debug stuff. The service sends the message to the activity as so…

                    MapPlot mapleg = new MapPlot();
                mapleg.fromPoint = LastGeoPoint;
                mapleg.toPoint = nextGeoPoint;              
                Gson gson = new Gson();
                String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string
                debugString = jsonString;
    
                //send the string to the activity
                Messenger messenger = (Messenger) extras.get("MESSENGER");
                Message msg = Message.obtain();  //this gets an empty message object
    
                msg.arg1 = 5;
                msg.obj = jsonString;
                try
                {
                    messenger.send(msg);
                }
                catch (android.os.RemoteException e1)
                {
                    Log.w(getClass().getName(), "Exception sending message", e1);
                }               
    

    I just picked the number 5, for now, as the message identifier. In this case I’m passing a complex class in a json string and then reconstrucing it in the activity.

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

Sidebar

Related Questions

I have Activity A, Activity B, and a static class in the same application.
i have an activity (ApplicationActivity) that call an intent service (DownloadService) The intentService download
I need help with this situation: I have activity, what starts IntentService. Service do
I have an Android application. In one activity I start a service like this:
I have two relatively simple codes. One main activity and one intent service. Main
in my app i have activity with XML layout, i want to build an
I have activity A in package one, and I want to run an intent
I have a custom dialog in my android application. I want to have in
I have activity A where authentication is done. After this user redirected to other
I have activity that load's image from server into imageview. Image is displayed after

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.