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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:33:39+00:00 2026-06-15T18:33:39+00:00

Which is better? To instantiate a Broadcast receiver inside an Activity or in Service

  • 0

Which is better? To instantiate a Broadcast receiver inside an Activity or in Service class, or make a class that extends BroadcastReceiver?

Below is an example where I instantiate BroadcastReceiver inside a Service class.

   public BroadcastReceiver receiver = new BroadcastReceiver() {
    private String filename;

    @Override
    public void onReceive(Context context, Intent intent){
        String action = intent.getAction();
        Bundle extras = intent.getExtras();
        filename = extras.getString("AudioPath");
    Toast.makeText(AudioService.this, "the audio file name sent: " + filename , Toast.LENGTH_LONG).show();
    if(action.equals("com.porno.xxx.AudioPlay")){
            selectedAudioPath = audiopath;
            String state = intent.getExtras().getString("stringdata");
            playSong();
            Toast.makeText(AudioService.this, "play audio from service string data "+ state, Toast.LENGTH_LONG).show();

        }
        else if(action.equals("com.porno.xxx.AudioPause")){
            pauseSong();

            selectedAudioPath = audiopath;
            Toast.makeText(AudioService.this, "pause audio from service", Toast.LENGTH_LONG).show();
        }
        else if(action.equals("com.porno.xxx.AudioSelector")){
            Toast.makeText(AudioService.this, "music selector from service", Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            audiopath = intent.getStringExtra("filename");
            Toast.makeText(AudioService.this, "selelcted audio path: " + audiopath, Toast.LENGTH_LONG).show();
        }
        else if(action.equals("com.porno.xxx.AudioRelease")){
            Toast.makeText(AudioService.this, "My Service Stopped and destoryed", Toast.LENGTH_LONG).show();
            player.stop();
            if (player != null) player.release();

                }   

        }

};
  • 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-15T18:33:40+00:00Added an answer on June 15, 2026 at 6:33 pm

    First you plan what you want to do with broadcast receiver. Then you analyze the best and feasible solutions.
    If you want to register and unregister the broadcast receiver inside the activity then your source code is ok.

    For example if you want to invoke the broadcast receiver when the application is not executing.(which means come out of application and not force close). Then you should not register and unregister the broadcast receiver in coding.
    For that you create/implement a seperate class extends from BroadcastReceiver.
    In manifest file you want to add the broadcast receiver.

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

Sidebar

Related Questions

I have created an abstract class FbActivity, which extends Activity to connect to facebook
Which is better? 1. = link_to Page, /page, :class => button, :data => {:theme
Which constitutes better object oriented design? Class User { id {get;set} } Class Office
Which is better way to use jQuery() ? I've heard that jQuery(element) doesn't ruin
I don't want to make this into a which is better... MVC or WebForms
I have a driver class called Advantech which is stored inside an ArrayList .
Say I have a class, which has a number of subclasses. I can instantiate
Which is the better approach public class Account { public UserAccount GetUserDetails(string acctId) {
I have a DB helper class which extends SQLiteOpenHelper and in onCreate, I want
which is better or faster, A or B? std::deque<Myclass> queue; ... // do something

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.