1

I have read the doc how to use LocalBroadcastManager?.

It works well in the SMSMain.java, and I can find the log info when I click btnToMore button in SMSMain.java:

Got message: This is A!

But I can't find the log info if I click btnNext button in StepName.java.

BTW, I open StepName.java by clicking the btnAddRule button in SMSMain.java .

SMSMain.java

package ui;
import info.dodata.smsforward.R;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import bll.PublicPar;

public class SMSMain extends ListActivity { 


    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Got message: " +message );
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_main);  

        Button btnAddRule = (Button) findViewById(R.id.btnAddRule);
        btnAddRule.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                Intent intent = new Intent().setClass(getApplicationContext(),
                        ui.StepName.class);
                startActivityForResult(intent, 20);
            }
        });

        Button btnToMore = (Button) findViewById(R.id.btnToMore);
        btnToMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                  Intent intent = new Intent(PublicPar.LocalBroadcastForRule);
                  intent.putExtra("message", "This is A!");
                  LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

            }           
        });

        findViewById(R.id.btnClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
             finish();
            }           
        });

    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                  new IntentFilter(PublicPar.LocalBroadcastForRule));
    }


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


}

StepName.java

package ui;
import info.dodata.smsforward.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import bll.PublicPar;

public class StepName extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_step_name); 


        Button btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {   

                Intent intent = new Intent(PublicPar.LocalBroadcastForRule);    
                intent.putExtra("message", "This is B!");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

                finish();
            }
        });     

    }
}
Community
  • 1
  • 1
HelloCW
  • 843
  • 22
  • 125
  • 310
  • if you use local broadcast or create new broadcast in your code, just use for that activity and if you want worked in every where you must create class and extends `BroadcastReceiver` – Shayan Pourvatan Nov 18 '13 at 11:01

1 Answers1

3

If you want to receive the LocalBroadcast in various different activities, you will have to register the receiver in all those receiving activities. The Sender activity sends/broadcasts notifications and the receiver activity that watches for notifications has to register the Broadcast Receiver for receiving the notifications.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thanks, but in http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager , it seems that they havn't to register the receiver in all those receiving activities. – HelloCW Nov 18 '13 at 11:14
  • Which answer on that question says that? Can you give the link of the answer please. See http://stackoverflow.com/a/8875292/1306419 answer on that question and others as well. For the receiver you will have to inform android that your activity also want to receive the notification when its sent. How else will the activity know unless you register for it? – Shobhit Puri Nov 18 '13 at 11:21
  • Shiki answered. it's the first answer. – HelloCW Nov 18 '13 at 14:03