【问题标题】:Calling Google Now search programmatically from Android application从 Android 应用程序以编程方式调用 Google Now 搜索
【发布时间】:2015-03-24 15:23:18
【问题描述】:

我有一个文本框和一个按钮。当我点击按钮时,Google Now 会触发文本框中的文本。我正在使用下面的函数来启动带有文本的 Google Now。

    public void searchWeb(String query)
    {
      Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
      intent.putExtra(SearchManager.QUERY, query);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
      startActivity(intent);
    }

问题是我只能在 Google Now 不处于运行状态时才能使用我的文本打开它。当它处于运行状态时,Google Now 页面会打开,但搜索查询不会更新为最新的。

我错过了什么吗?

【问题讨论】:

    标签: android android-intent google-now


    【解决方案1】:

    进一步调查这个问题,我发现我错误地使用了设置标志方法。在这里,我必须使用 addflags 方法而不是 setFlags,因此代码可以正常运行。

        public void searchWeb(String query)
        {
          Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
          intent.putExtra(SearchManager.QUERY, query);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
          startActivity(intent);
        }
    

    【讨论】:

    • 你错过了理解这个问题,你的代码现在是谷歌的,我正在寻找的是谷歌助手在安卓 7.x 的 Pixel 手机中引入的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多