【问题标题】:Intent not received by activity活动未收到意图
【发布时间】:2011-03-02 23:10:54
【问题描述】:

我有一个主要活动,它启动一项服务以在后台进行网络搜索,我希望主要活动在搜索完成后获得一个意图。

在我的主要活动中,我定义了一个 BroadcastReceiver 和一个 Intent Filter 来监听“搜索结束”的意图:

public class AgeRage extends Activity {


    // Listener to all results from background processes

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {  
            if (intent.getAction().equals(ImageSearchService.SEARCH_RESULT_ACTION)) {
0);
                Toast.makeText(context,"Got " + i + "results", Toast.LENGTH_SHORT).show();
            }
            else Toast.makeText(context,"unknown intent", Toast.LENGTH_SHORT).show();


        }
    };

    IntentFilter receiverFilter = new IntentFilter ();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        // Register to image search service messages

        receiverFilter.addAction(ImageSearchService.SEARCH_RESULT_ACTION);
        registerReceiver(receiver,receiverFilter);

       ...

在服务中,我进行搜索,完成后,我发送一个 Intent:

public class ImageSearchService extends IntentService {

...

protected void onHandleIntent (Intent intent) {

... doing search ...

    Intent i = new Intent (this,AgeRage.class);
    i.setAction (SEARCH_RESULT_ACTION);
    i.putExtra(SEARCH_STATUS, (searchStatus ==SearchStatus.DONE) ? true:false);
    i.putExtra (SEARCH_RESULT_NUM, totalResultNum);
    i.putExtra (SEARCH_ID, searchID);
    sendBroadcast (i,null);
}

但是,主要活动没有获得意图。我知道正在调用 sendBroadcast 而不是接收者的 OnReceive(使用调试器检查)。

我假设由于我是动态创建过滤器的,我不需要在清单文件中定义过滤器。

我做错了吗?

谢谢 艾萨克

【问题讨论】:

标签: android android-intent broadcastreceiver intentfilter


【解决方案1】:

好的。好吧,我刚刚检查了我的,我们正在以相同的方式进行操作,但是...

ImageSearchService.SEARCH_RESULT_ACTION

尝试做 com.yourpackagename.ImageSearchSrvi​​ce.SEARCH_RESULT_ACTION

其中 SEARCH_RESULT_ACTION 是一个公共静态字符串变量。看看有没有帮助。

我想一定是ACTION的命名。另请注意,您可能希望运行断点并检查日志。执行 intent.getAction() 并将其打印出来,而不是在 if 语句中进行检查。只是总是打印出来看看。不需要打破广播接收器内部,它会在一段时间后崩溃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多