Android的广播接收机和意图过滤器 [英] Android-Broadcast Receiver and Intent Filter
问题描述
我是新到Android platform.please帮我广播接收器和意图过滤器的行为在android.please在简单的线条或提前example.thanks解释...
I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.please explain in simple line or with example.thanks in advance...
推荐答案
一个广播接收器是一类在你的Android项目负责使用机器人接收所有的意图,这是由其他的活动发.content.ContextWreapper.sendBroadcast(意向意图)
A broadcast receiver is a class in your Android project which is responsible to receive all intents, which are sent by other activities by using android.content.ContextWreapper.sendBroadcast(Intent intent)
在你receicving活动的清单文件,你必须声明这是你的广播接收器类,例如:
In the manifest file of you receicving activity, you have to declare which is your broadcast receiver class, for example:
<receiver android:name="xyz.games.pacman.network.MessageListener">
<intent-filter>
<action android:name="xyz.games.pacman.controller.BROADCAST" />
</intent-filter>
</receiver>
正如你所看到的,你也在这里定义的意图过滤器,那就是,这应该由broadcas接收机接收意向。
As you can see, you also define the intent filter here, that is, which intents should be received by the broadcas receiver.
然后你必须定义延伸了BroadcastReceiver的类。这是您在清单文件中定义的类:
Then you have to define a class which extends BroadcastReceiver. This is the class you defined in the manifest file:
public class MessageListener extends BroadcastReceiver {
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
...
}
下面,这是通过过滤器所有的意图都收到,你可以使用的方法调用传递的参数访问它们。
Here, all intents which are passed through the filter are received and you can access them using the parameter passed in the method call.
这篇关于Android的广播接收机和意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!