开发者

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?


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜