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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:23+00:00 2026-06-13T03:03:23+00:00

I’m trying to return the result from an IntentSerivce to the mainactivity using an

  • 0

I’m trying to return the result from an IntentSerivce to the mainactivity using an intent, but I can’t get it to work.
The IntentService receives the intent from the activity without a problem, does it’s thing and gets a JSONstring. Now the only problem left is to send this String back to the activity.

Here is the method in the mainactivity:

public String RTConn(String query){
    System.out.println("Querying Rotten Tomatoes.");
    Intent rtIntent = new Intent(MainActivity.this, RTConnection.class);
    rtIntent.putExtra("query", query);
    bindService(rtIntent, RTConnection, Context.BIND_AUTO_CREATE);
    startService(rtIntent);
    String json = getIntent().getStringExtra("json");
    return json;

And here is the IntentService:

public void onStart(Intent intent, int startId){
    System.out.println("intent Received");
    String jsonString = rottenTomatoesSearch(intent.getStringExtra("query"));
    Intent RTRetur = new Intent(RTConnection.this, MainActivity.class);
    RTRetur.putExtra("json", jsonString);
    startActivity(RTRetur);
}

Obviously the startActivity isn’t working, but what else is there to use? (I would rather not use a broadcast receiver).
Regards Peter L.

Update

this it how the MainActivity looks atm:

public static final String RECEIVE_JSON = "student.kau.RECEIVE_JSON";
private BroadcastReceiver RTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("broadcast Received: "+intent.getAction());
        if(intent.getAction().equals(RECEIVE_JSON)){
            System.out.println("JSON Received.");
            String serviceJsonString = intent.getStringExtra("json");
        }
    }
};

This code is the onCreate:

LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(RECEIVE_JSON);
    bManager.registerReceiver(RTReceiver, intentFilter);

This is the method:

public void onStart(Intent intent, int startId){
    System.out.println("intent Received");
    String query = intent.getStringExtra("query");
    String jsonString = rottenTomatoesSearch(query);
    System.out.println("Fetching done");
    Intent RTRetur = new Intent(MainActivity.RECEIVE_JSON);
    System.out.println("action: "+RTRetur.getAction());
    RTRetur.putExtra("json", jsonString);
    sendBroadcast(RTRetur);

}

The last trace in logcat is the System.out.println("action: "+RTRetur.getAction()");

-action: 

    student.kau.RECEIVE_JSON
  • 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-13T03:03:23+00:00Added an answer on June 13, 2026 at 3:03 am

    For this, you can use a BroadcastReceiver in your Activity.

    Here is a great example I use to communicate continuously between Service <> Activity using BroadcastReceivers.

    Here is another great example of communication between Service <> Activity. It uses Messenger and IncomingHandler.

    BroadcastReceiver

    I will make a quick example for your case.

    This is your BroadcastReceiver for your Activity. It is going to receive your String:

    //Your activity will respond to this action String
    public static final String RECEIVE_JSON = "com.your.package.RECEIVE_JSON";
    
    private BroadcastReceiver bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(RECEIVE_JSON)) {
                String serviceJsonString = intent.getStringExtra("json");
                //Do something with the string
            }
        }
    };
    LocalBroadcastManager bManager;
    

    In your onCreate() of the activity

    bManager = LocalBroadcastManager.getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(RECEIVE_JSON);
    bManager.registerReceiver(bReceiver, intentFilter);
    

    In your onDestroy() of the activity, make sure you unregister the broadcastReceiver.

    bManager.unregisterReceiver(bReceiver);
    

    And finally, in your Service onStart(), do this:

    System.out.println("intent Received");
    String jsonString = rottenTomatoesSearch(intent.getStringExtra("query"));
    Intent RTReturn = new Intent(YourActivity.RECEIVE_JSON);
    RTReturn.putExtra("json", jsonString);
    LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
    

    and your Activity will receive the intent with that json in it as an extra

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.