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

  • Home
  • SEARCH
  • 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 7857401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:02:20+00:00 2026-06-02T21:02:20+00:00

I have written a simple activity to test out services and broacast receivers and

  • 0

I have written a simple activity to test out services and broacast receivers and a service to go along with it. In order to know whether or not it’s working I’ve set up a Toast within the main activity to be showed once the OnReceive() method is called. But for the life of me I can’t get this to work.

These are the codes:

public class ServicesAndBroadcastIntentActivity extends Activity {

private Toast test;
private Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    intent = new Intent(this,serviceD.class);
    test = Toast.makeText(this,"Test",Toast.LENGTH_LONG);
    test.setGravity(Gravity.CENTER,0,0);
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        test.setText((intent.getStringExtra("EXTRA_MSG")));
        test.show();
    }
};


@Override
public void onResume(){
    super.onResume();
    startService(intent);
    registerReceiver(broadcastReceiver, new IntentFilter(serviceD.BROADCAST_ACTION));
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
    stopService(intent);        
}

}

public class serviceD extends Service{

private Intent intent;
static final String BROADCAST_ACTION = "com.mejg.ServicesAndBroadcastIntent";

public void onCreate() {
    super.onCreate();
    intent = new Intent(BROADCAST_ACTION);  
}

public void onStart(){
    intent.putExtra("EXTRA_MSG","hola");
    sendBroadcast(intent);
    stopSelf();
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

  • 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-02T21:02:24+00:00Added an answer on June 2, 2026 at 9:02 pm

    You are calling startService() before registerReceiver(). Both are asynchronous operations, but they will still likely occur in sequence. Hence, onStart() of your service will be called before registerReceiver() does its work, which means your broadcast goes out before your receiver is set up.

    For this sort of experimentation, I recommend setting up a basic UI (e.g., one really big button) and doing the startService() call when the button is pressed.

    Also, since the service calls stopSelf(), you do not need to call stopService() from the activity.

    Also also, you might consider using LocalBroadcastManager for this — same basic syntax with better performance and security, since it all stays within your process.

    UPDATE

    Also also also, onStart() has been deprecated for two-plus years, and your method signature for it is wrong, anyway. Please use onStartCommand(), with the right parameters.

    Also also also also, use @Override when overriding methods, to help you catch these sorts of problems.

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

Sidebar

Related Questions

I have written a simple WCF service that accepts and stores messages. It works
I have a very simple VB.net Windows Service written using VS.net 2008. The program
I have written a simple test application using asp.net mvc with C#. The application
I have written a simple test class for Play! 2.0: public class TestLogin {
I have written a simple WPF application in which I wanted to test the
I'm making a simple Android game written in Java. I have my activity... public
I have written a simple Cocoa app. In Activity Monitor, it is shown to
I have written a simple WCF web service which is configured programmaticaly. It exposes
I have written a simple geocoding application but it is not working.I have given
I have written simple code to get content from xml file to php. $xml

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.