来自小部件提供商的发布意图操作

发布于 2024-09-12 06:07:22 字数 1224 浏览 2 评论 0原文

我想构建一个搜索小部件。单击小部件应在我的应用程序内打开搜索活动。这是来自小部件提供商的 onUpdate() 的代码。

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    /* as their may be many widget instances for this widget. we get an array. */
    for(int i=0;i< appWidgetIds.length; i++)
    {
        Intent intent = new Intent(context, SearchActivity.class);
        intent.setAction("android.intent.action.SEARCH");
        PendingIntent pdIntent = PendingIntent.getActivity(context, 0, intent, 0);

        RemoteViews view = new RemoteViews(context.getPackageName(), myPackage.R.layout.search_appwidget); 
        view.setOnClickPendingIntent(R.id.search_widget_textbox,pdIntent );
        appWidgetManager.updateAppWidget(appWidgetIds[i], view);
    }

}

在我的 SearchActivity.onCreate() 中,我正在检查意图操作 Intent.ACTION_SEARCH.equals( this.getIntent().getAction())。但是,当通过小部件提供程序使用 PendingIntent 发布意图时,this.getIntent().getAction() 返回 null。当通过 SearchManager 调用 SearchActivity(应用程序的默认 SearchManager 处理程序)时,它会获取有效的操作,如 android.intent.action.SEARCH

我对待定意图做错了什么?

I want to build a search widget. Clicking on the widget should open search activity inside my app. Here is the code from widget provider's onUpdate().

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    /* as their may be many widget instances for this widget. we get an array. */
    for(int i=0;i< appWidgetIds.length; i++)
    {
        Intent intent = new Intent(context, SearchActivity.class);
        intent.setAction("android.intent.action.SEARCH");
        PendingIntent pdIntent = PendingIntent.getActivity(context, 0, intent, 0);

        RemoteViews view = new RemoteViews(context.getPackageName(), myPackage.R.layout.search_appwidget); 
        view.setOnClickPendingIntent(R.id.search_widget_textbox,pdIntent );
        appWidgetManager.updateAppWidget(appWidgetIds[i], view);
    }

}

Inside my SearchActivity.onCreate(), I am checking for intent action as Intent.ACTION_SEARCH.equals( this.getIntent().getAction()). However, when an intent is posted using PendingIntent via widget provider the this.getIntent().getAction() returns null. When SearchActivity (which is a default SearchManager handler for the app) is invoked via SearchManager it gets valid action as android.intent.action.SEARCH.

What am I doing wrong with pending intent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮坠入山谷 2024-09-19 06:07:22

我在这里得到了答案。
因为它表明该操作已在 SearchActivity.OnReceive() 中发布。此外,该操作适用于 text_box(不适用于 Activity),因此 SearchActivity.getIntent().getAction() 为 null。

I got my answer here.
As it indicates the action is posted in SearchActivity.OnReceive(). Also the action is for text_box (not for activity) and hence SearchActivity.getIntent().getAction() is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文