简体   繁体   中英

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!

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

check this tutorial here you will get all help about broadcast including how to start service from activity or vice versa

http://www.vogella.de/articles/AndroidServices/article.html

For that you have to broadcast a intent for the receiver, see the code below :-

Intent intent=new Intent();
getApplicationContext().sendBroadcast(intent);

You can set the action and other properties of Intent and can broadcast using the Application context, Whatever action of Intent you set here that you have to define in the AndroidManifest.xml with the receiver tag.

Check this answer:

https://stackoverflow.com/a/5473750/928361

I think if you don't specify anything in the IntentFilter , you need to tell the intent the receiver class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM