在搜索请求中使用Intent而不是事件侦听器启动是否有优势?

时间:2015-07-15 17:21:27

标签: android android-search

我看了过来,找不到简单的答案。在搜索框上使用Intent而非事件监听器将文本发送到查询事件是否有优势?

SearchView searchView =
            (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if(searchView != null){
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String s) {
                Toast.makeText(getApplicationContext(),
                        "String entered is " + s, Toast.LENGTH_SHORT).show();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }

使用:

 private void handleIntent(Intent intent){
    if(Intent.ACTION_SEARCH.equals(intent.getAction())){
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.d(LOG_TAG, "QUERY: " + query);

        new FetchArtistTask().execute(query);

    }
}

1 个答案:

答案 0 :(得分:1)

意图的优点:

  • 可以将Intent定向到除SearchView之外的其他活动;
  • 您可以让单个活动处理来自多个其他活动的搜索请求;
  • 在处理Intent时,您可以充分利用不同的Activity启动模式和任务堆栈;
  • 除了使用SearchView之外,搜索Intent还可以使用“搜索”对话框在“活动”之外触发(例如在Dialogs和PopupWindows中),使您的应用程序中的搜索体验更加统一;
  • 当用户点击Notification / appwidget等时,您可以自行搜索意图从服务发送。

听众的好处:

  • 可用于过滤建议列表作为用户类型。

这两种方法并不相互排斥,因此您可以同时使用这两种方法。