Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动未收到意图_Android_Android Intent_Broadcastreceiver_Intentfilter - Fatal编程技术网

Android 活动未收到意图

Android 活动未收到意图,android,android-intent,broadcastreceiver,intentfilter,Android,Android Intent,Broadcastreceiver,Intentfilter,我有一个主活动,它启动一个服务,在后台进行web搜索,我希望主活动在搜索完成时获得一个意图 在我的主要活动中,我定义了一个BroadcastReceiver和一个意图过滤器来监听搜索意图的结束: public class AgeRage extends Activity { // Listener to all results from background processes BroadcastReceiver receiver = new BroadcastReceiv

我有一个主活动,它启动一个服务,在后台进行web搜索,我希望主活动在搜索完成时获得一个意图

在我的主要活动中,我定义了一个BroadcastReceiver和一个意图过滤器来监听搜索意图的结束:

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

       ...
在服务中,我进行搜索,搜索完成后,我发送一份意向:

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

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

我做错什么了吗

谢谢
艾萨克

好的。我刚刚检查了我的,我们也是这么做的,但是

ImageSearchService.SEARCH\u结果\u操作

尝试执行com.yourpackagename.ImageSearchSrvice.SEARCH\u RESULT\u操作

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


我想这一定是动作的命名。还请注意,您可能希望运行tru断点,只需检查日志即可。执行intent.getAction并将其打印出来,而不是在if语句中进行检查。总是把它打印出来看看。不需要在broacast接收器内部断开,过一段时间就会崩溃。

不确定您的问题您实际上是在使服务交互比需要的更复杂。由于该服务是本地服务,您可以直接与它对话,并让它在活动完成时通知您的活动,而无需广播意图并使用接收器收听。谢谢,我刚把我的代码改成与接收器一起工作,它工作得非常好