theITRanger22
theITRanger22

Reputation: 184

How to launch a broadcast receiver?

I have a class that extends the broadcast receiver. My question is how will I go about calling on this activity in another class... I tried to create a intent for it but I kept getting a syntax error. Is it another way to start the broadcast receiver?

Upvotes: 1

Views: 5068

Answers (2)

Rohit Sharma
Rohit Sharma

Reputation: 13815

IntentFilter filter = new IntentFilter("com.mydefinepackage.myactivity");
this.registerReceiver(new Receiver(), filter);

Declare this private class and use above code within myactivity Activity.

private class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        myOwnMethod();
    }
}

Execute this code from any other activity. myOwnMethod will be called then.

Intent i =new Intent("com.mydefinepackage.myactivity");
sendBroadcast(i);

Upvotes: 2

Snicolas
Snicolas

Reputation: 38168

You have to use

 Intent intent = new Intent( "mypackage.myaction" );
 activity.sendBroadCast( intent );

Where activity is the one that launches your BroadcastReceiver and Intent, an intent that matches the filter of your BroadcastReceiver in your manifest file.

You will do something that looks like :

<receiver android:name="your broadcast receiver class" android:label="a name">
    <intent-filter>
        <action android:name="mypackage.myaction" />
    </intent-filter>
</receiver>

Regards, Stéphane

Upvotes: 2

Related Questions