0

myButton is a button that when clicked is supposed to receive a broadcast from a background IntentService. But the broadcast is never received. However if I move the broadcastReceiver outside of myButton.setOnClickListener function, then I begin to receive broadcasts from my background service.

Is there a way to make the broadcastReceiver receive broadcasts within the setOnClickListener function?

public class MainActivity extends Activity {

private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    myButton = (Button)findViewById(R.id.button1);
    myButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            broadcastReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent intent) {

                    Toast.makeText(MainActivity.this, "BROADCAST RECEIVED", Toast.LENGTH_SHORT).show();
                    stopService(msgIntent);
                }
            };

        }
    });

  public void onResume()
  {
    super.onResume();

    IntentFilter filter = new IntentFilter(SimpleIntentService.ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(broadcastReceiver,filter);


  }

  public void onPause()
  {
    unregisterReceiver(broadcastReceiver);
    super.onPause();
  }

}

I had to take out the broadcastReceiver from onClick method. This works and broadcast is received:

public class MainActivity extends Activity {

    private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    myButton = (Button)findViewById(R.id.button1);
    myButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //do extra stuff 
        }
    });
    broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent intent) {

                Toast.makeText(MainActivity.this, "BROADCAST RECEIVED", Toast.LENGTH_SHORT).show();
                stopService(msgIntent);
            }
        };
    }
}
Dobob
  • 337
  • 1
  • 3
  • 12

1 Answers1

0

Do you forget to registerReceiver?

You might also need to assign a IntentFilter when you register a receiver.

The following is some sample codes from my project:

private class LocationInfoReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // do something
    }
}

locationInfoReceiver = new LocationInfoReceiver();
// the key you use setAction() method in your Intent Service
IntentFilter locationInfoReceiverFilter = new IntentFilter("your key");
locationInfoReceiverFilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(locationInfoReceiver, locationInfoReceiverFilter);
Carter Chen
  • 792
  • 1
  • 8
  • 22
  • I have register receiver in the onResume() function. Also tried registering it before the onClick function and it didn't work. Only taking the broadcast receiver outside of the onClick method works. I edited in the onResume and onPause methods. – Dobob Oct 24 '15 at 06:21
  • Where do you start your service? I can't see it in your code. By the way, according to [this post](http://stackoverflow.com/questions/7191109/difference-between-code-before-and-after-super), the order you put super.onPause in the override method "onPause" may cause some problem, maybe you can move it backward and try it again. – Carter Chen Oct 24 '15 at 07:45