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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:24:02+00:00 2026-06-07T11:24:02+00:00

I’d like to know how to pass data between a Service and an Activity

  • 0

I’d like to know how to pass data between a Service and an Activity without broadcastReceiver.

This is what I can’t make:

  1. I have a Service that periodically sends an intent via broadcast.
  2. I want to get the extras of this intent from an activity when I want, without wait a broadcast receiver.

I tried to use this code:

  \\Service
  String BROADCAST_ACTION = "ACTION";
  Intent sendToUI = new Intent(BROADCAST_ACTION);
  sendToUI.putExtra("key", "value");
  sendBroadcast(sendToUI);


  \\Activity
  IntentFilter iF = new IntentFilter(MyService.BROADCAST_ACTION);
  Intent intent = c.registerReceiver(null, iF);
  Bundle extras = intent.getExtras();
  if(extras != null){
      String string = intent.getStringExtra("key");
 }

But I get a nullpointerexception because intent is always null (I get nullpointerexception NOT in Bundle extras BUT IN Intent intent LINE).

  • 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-07T11:24:04+00:00Added an answer on June 7, 2026 at 11:24 am

    In order to do this like this, the broadcast you send periodically has to “hang around” so that the activity can get it when it wants to.

    In your service you need to send this broadcast as “sticky”, like this:

    // Service
    String BROADCAST_ACTION = "ACTION";
    Intent sendToUI = new Intent(BROADCAST_ACTION);
    sendToUI.putExtra("key", "value");
    sendStickyBroadcast(sendToUI);
    

    You must hold the BROADCAST_STICKY permission in order to use this API. If you do not hold that permission, SecurityException will be thrown.

    EDIT: Add code for the Activity to read it:

    // Activity
    IntentFilter iF = new IntentFilter(MyService.BROADCAST_ACTION);
    Intent intent = c.registerReceiver(null, iF);
    if (intent != null && intent.hasExtra("key")) {
        String string = intent.getStringExtra("key");
        // Now do something with it...
    }
    

    Also, I’d suggest that you change the string you are using for `MyService.BROADCAST_ACTION” to include your fully qualified package name. This is because if you use just “ACTION”, there may be other applications that are also sending around sticky broadcasts with that action and you won’t be sure to get the one you intended (ie: the one sent by your service). Use something like this (in your service class):

    String BROADCAST_ACTION = "com.mycompany.myapplication.ACTION";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I know there's a lot of other questions out there that deal with this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.