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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:41:56+00:00 2026-06-01T09:41:56+00:00

I would like to start a broadcast receiver from an activity. I have a

  • 0

I would like to start a broadcast receiver from an activity. I have a Second.java file which extends a broadcast receiver and a Main.java file from which I have to initiate the broadcast receiver.
I also tried doing everything in Main.java as follows but didn’t know how to define in manifest file…

Main.java:

public class Main extends Activity {
/** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
String rec_data = "Nothing Received";
private BroadcastReceiver myReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if( intent.getStringExtra("send_data")!=null)
                    rec_data = intent.getStringExtra("send_data");
            Log.d("Received Msg : ",rec_data);
        }
    }; 
    }
    protected void onResume() {
        IntentFilter intentFilter = new IntentFilter();
        //intentFilter.addDataType(String);
        registerReceiver(myReceiver, intentFilter);
        super.onResume();
    }

  @Override
protected void onPause() {
// TODO Auto-generated method stub
    super.onPause();
    this.unregisterReceiver(this.myReceiver);
}

}

If I cannot do everything in one class as above, how can I call the Broadcast Receiver from Main.java? Can anyone please let me know where I’m doing it wrong? Thanks!

  • 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-01T09:41:58+00:00Added an answer on June 1, 2026 at 9:41 am

    use this why to send a custom broadcast:

    Define an action name:

    public static final String BROADCAST = "PACKAGE_NAME.android.action.broadcast";
    

    AndroidManifest.xml register receiver :

    <receiver android:name=".myReceiver" >  
        <intent-filter >  
            <action android:name="PACKAGE_NAME.android.action.broadcast"/>  
        </intent-filter>  
    </receiver> 
    

    Register Reciver :

    IntentFilter intentFilter = new IntentFilter(BROADCAST);
    registerReceiver( myReceiver , intentFilter);
    

    send broadcast from your Activity :

    Intent intent = new Intent(BROADCAST);  
            Bundle extras = new Bundle();  
            extras.putString("send_data", "test");  
            intent.putExtras(extras);  
            sendBroadcast(intent);
    

    YOUR BroadcastReceiver :

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                 Bundle extras = intent.getExtras();
               if (extras != null){  
               {
                        rec_data = extras.getString("send_data");
                Log.d("Received Msg : ",rec_data);
                }
            }
        };
    

    for more information for Custom Broadcast see Custom Intents and Broadcasting with Receivers

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

Sidebar

Related Questions

I would like to Start a new activity from my custom dialog, I have
I would like start a long poll request from javascript which is fine and
I have created a jabberbot class that I would like to broadcast scheduled messages.
I would like to start an application from a pre-build step. However, I do
I would like to start a method from my Service with pressing a physical
would like to start the PDF-Viewers from an Stream instead of an FileLocation ()
I'm trying to start a status bar notification from a broadcast receiver and then
I have an activity which displays some data fetched from the server. If no
I would like start with .NET Micro Framework as my hobby project. What hardware
I would like to start tagging my deployed binaries with the latest SVN revision

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.