基于自定义数据过滤器意向 [英] Filter Intent based on custom data

查看:103
本文介绍了基于自定义数据过滤器意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要广播的意图与自定义的数据,只有具有这种自定义的数据应该收到此意向的接收器,怎么可以这样做?

这我怎么播的意图:

 意向意图=新的意图();
 intent.setAction(为com.example);
 context.sendBroadcast(意向);
 

和我定义的广播接收机如下:

 <接收器的Andr​​oid版本:NAME =com.test.myReceiver机器人:启用=真正的>
        <意向滤光器>
            <作用安卓名=为com.example>< /作用>
        &所述; /意图滤光器>
    < /接收器>
 

解决方案

设置你的myReceiver类基本anmustangs如何公布。

 公共类myReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(背景为arg0,意图意图){
        //做一点事
    }
}
 

您不应该检查意图行动,因为根据你所包含在你的清单中,至少在这个简单的情况下,你已经过滤它。你可以有其他的动作和过滤器在这种情况下,你需要有某种检查所接收到的意图。

如果您希望过滤器上的数据进行筛选,对意图过滤器的SDK文档覆盖了。我不是伟大的数据类型,所以任何例子,我会给会比较差。在任何情况下,这里是一个链接到清单意图过滤器页面: http://developer.android.com/guide/topics/manifest /intent-filter-element.html

和对数据元素的特定页面; http://developer.android.com/guide/topics/manifest/data -element.html

I want to broadcast intent with custom-data and only the receiver that have this custom data should receive this intent, how this could be done ?

this how i broadcast the intent :

Intent intent = new Intent();
 intent.setAction("com.example");
 context.sendBroadcast(intent);

and i define the broadcast receiver as following :

 <receiver android:name="com.test.myReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="com.example"></action>
        </intent-filter>
    </receiver>

解决方案

Setup your myReceiver class basically how anmustangs posted.

public class myReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent intent) {
        //Do something
    }
}

You shouldn't have to check the Intent action because you've already filtered for it based on what you've included in your manifest, at least in this simple case. You could have other actions and filters in which case you'd need to have some kind of check on the received intent.

If you want the filter to filter on data, the sdk documentation on intent filters covers that. I'm not great with data types so any example I would give would be relatively poor. In any event, here is a link to the manifest intent filter page: http://developer.android.com/guide/topics/manifest/intent-filter-element.html

And the specific page for the data element; http://developer.android.com/guide/topics/manifest/data-element.html

这篇关于基于自定义数据过滤器意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆