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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:40:53+00:00 2026-06-10T06:40:53+00:00

Hi all and thank you for your help! I’ll start with the code So,

  • 0

Hi all and thank you for your help!
I’ll start with the code
So, the class with the broadcast receiver is this:

public class MyService extends Service {

    // ...
    // ACTION
    public static final String action = "com.mywebsite.myapp.package.class.action";
    // ...
    public void onCreate() {
        // SET BROADCAST RECEIVER
        broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.w("broadcast receiver", "action: " + action);
                if (action.equals("**")) {
                    Log.w("broadcast receiver", "**");
                }
            }
        };
        // REGISTER BROADCAST
        final IntentFilter myFilter = new IntentFilter();
        myFilter.addAction(action);
        registerReceiver(this.broadcastReceiver, myFilter);
    }
    // ....
}

And I try to send a broadcast in this way from a fragment

Intent myIntent = new Intent(getActivity()
        .getApplicationContext(), MediaPlayerService.class);
getActivity().startService(myIntent);
myIntent = new Intent(getActivity().getApplicationContext(),
        MediaPlayerService.class);
myIntent.setAction(MyService.action);
myIntent.putExtra("data", "*******");
getActivity().sendBroadcast(myIntent);

However the broadcast receiver is never called. I can say this because of the logcat: the line Log.w(“broadcast receiver”, “action: ” + action); is never called. How can I resolve?

Thank you!

EDIT: class code:

public class MediaPlayerService extends Service {

    private MediaPlayer mediaPlayer = null;
    private AudioManager audioManager;
    private BroadcastReceiver broadcastReceiver;
    private String absoluteFilePath;
    private Boolean areThereAnyErrors = false;
    private int savedVolume;
    // ACTIONS
    public static final String prepareAndPlayNewFile = "com.disgustingapps.player.AudioManagement.MediaPlayerService.prepareAndPlayNewFile";

    @Override
    public void onCreate() {
        Log.w("MediaPlayerService", "onCreate called");
        // SET BROADCAST RECEIVER
        broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.w("broadcast receiver", "action: " + action);
                if (action.equals("prepareAndPlayNewFile")) {
                    Log.w("broadcast receiver", "prepareAndPlayNewFile");
                    prepareAndPlayNewFile(intent
                            .getStringExtra("absoluteFilePath"));
                }
            }
        };
        // REGISTER BROADCAST
        final IntentFilter myFilter = new IntentFilter();
        myFilter.addAction(prepareAndPlayNewFile);
        registerReceiver(this.broadcastReceiver, myFilter);
    }

    @Override
    public void onDestroy() {
        Log.w("MediaPlayerService", "onDestroy called");
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("MediaPlayerService", "onStartCommand called");
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    public void prepareAndPlayNewFile(String absoluteFilePath) {
        // ...do 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-06-10T06:40:54+00:00Added an answer on June 10, 2026 at 6:40 am

    The reason you are not receiving the Intent is because the receiver is in onCreate().
    context.startService() will only pass the Activity‘s Intent to the Service‘s onStartCommand(). The Service has already been created by then (or should have), so trying to receive in onCreate() will never work.

    More info here

    Trying to receive in onStartCommand() will only work once (guess). But having a BroadcastReceiver in onStartCommand will only get you the data if you call startService(). Since you are calling this anyway, it’s far easier just do ditch the BroadcastReceiver and just pass the data with the Intent delivered to the Services onStartCommand().

    EDIT

    @Override
    public void onCreate() {
        Log.w("MediaPlayerService", "onCreate called");
    }
    
    @Override
    public void onDestroy() {
        Log.w("MediaPlayerService", "onDestroy called");
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("MediaPlayerService", "onStartCommand called");     
    
        String action = intent.getAction();
        Log.w("onStartCommand()", "action: " + action);
    
        if (action.equals("prepareAndPlayNewFile")) {
           prepareAndPlayNewFile(intent.getStringExtra("absoluteFilePath"));
        }
    
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.   
        return START_STICKY;
    }
    

    Then just do this:

    Intent myIntent = new Intent(getActivity().getApplicationContext(), MediaPlayerService.class);
    myIntent.setAction("prepareAndPlayNewFile");
    myIntent.putExtra("absoluteFilePath", "/path/");
    startService(myIntent);
    

    startService():

    Every call to this method will result in a corresponding call to the target service’s onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

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

Sidebar

Related Questions

UPDATE: Thank you all so much for your help on this. I've taken a
EDIT: Thank you all for your help. I edited my Database class to contain
EDIT2: Thank you all for your help! EDIT: on adding @staticmethod, it works. However
EDIT 2 Thank you all for your help! By fusing the answers and some
first of all, thank you for your help in forward. I'm using Python and
First, thank you all for your help. I'm trying to locate something similar to
Hi first I like to thank you all for your help We have to
Thank you all for your help. A number of you posted (as I should
Problem Solved, thank you all for your help! I've run into a problem that
Thanks for your help! I'd like to output all companyName entries that have uploads

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.