Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

broadcastreceiver - Start Broadcast Receiver from an activity in android

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...