Android:向Intent添加数据无法加载Activity

发布于 2024-08-28 10:44:46 字数 1098 浏览 5 评论 0原文

我有一个小部件,当用户单击小部件主体时,它应该调用主应用程序的活动。我的设置适用于单个小部件实例,但对于同一小部件​​的第二个实例,PendingIntent 会被重用,因此我作为额外发送的重要信息会被第一个实例覆盖。因此,我认为我应该将小部件 ID 作为 Intent 数据传递,但是一旦添加 Intent#setData,我就会在日志中看到 2 个单独的 Intent 被适当触发,但是活动无法拾取它,因此基本上活动不会出现并且没有任何反应(没有错误或警告以太) 下面是在清单中设置 Activity 的方式:

    <activity android:name=".SearchResultsView" 
         android:label="@string/search_results"
        <intent-filter>
            <action android:name="bostone.android.search.RESULTS" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

这是设置用于处理点击的代码

Intent di = new Intent("bostone.android.search.RESULTS");
di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// if line below is commented out - the Activity will start
di.setData(ContentUris.withAppendedId(Uri.EMPTY, widgetId));
di.putExtra("URL", url);
views.setOnClickPendingIntent(R.id.widgetContent, 
    PendingIntent.getActivity(this, 0, di, 0));

主应用程序和小部件被打包为 2 个单独的 APK,每个 APK 都在自己的包和清单中

I have a widget that supposed to call an Activity of the main app when the user clicks on widget body. My setup works for a single widget instance but for a second instance of the same widget the PendingIntent gets reused and as result the vital information that I'm sending as extra gets overwritten for the 1st instance. So I figured that I should pass widget ID as Intent data however as soon as I add Intent#setData I would see in the log that 2 separate Intents are appropriately fired but the Activity fails to pick it up so basically Activity will not come up and nothing happens (no error or warning ether)
Here's how the activity is setup in the Manifest:

    <activity android:name=".SearchResultsView" 
         android:label="@string/search_results"
        <intent-filter>
            <action android:name="bostone.android.search.RESULTS" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

And here's code that is setup for handling the click

Intent di = new Intent("bostone.android.search.RESULTS");
di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// if line below is commented out - the Activity will start
di.setData(ContentUris.withAppendedId(Uri.EMPTY, widgetId));
di.putExtra("URL", url);
views.setOnClickPendingIntent(R.id.widgetContent, 
    PendingIntent.getActivity(this, 0, di, 0));

The main app and the widget are packaged as 2 separate APK each in its own package and Manifest

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

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

发布评论

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

评论(1

來不及說愛妳 2024-09-04 10:44:46

我认为您需要在 中使用 标记,以便您触发的意图与您注册的意图过滤器相匹配。

https://developer.android.com/guide/topics/manifest/ data-element.html

使用 Uri.EMPTY 也可能是一个问题。我会创建你自己的 Uri 方案,以便你的 setData() 调用看起来像:

di.setData(Uri.withAppendedPath(Uri.parse("droidln://widget/id/"), String.valueOf(appWidgetId)));

并且你的意图过滤器看起来像:

    <intent-filter>
        <action android:name="bostone.android.search.RESULTS" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="droidln"/>
    </intent-filter>

I think you need a <data> tag in your <intent-filter> in order for the intent you are firing to match the intent-filter you have registered.

https://developer.android.com/guide/topics/manifest/data-element.html

Also using Uri.EMPTY may be a problem. I'd create your own Uri scheme, so that your setData() call looks something like:

di.setData(Uri.withAppendedPath(Uri.parse("droidln://widget/id/"), String.valueOf(appWidgetId)));

and your intent-filter would look like:

    <intent-filter>
        <action android:name="bostone.android.search.RESULTS" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="droidln"/>
    </intent-filter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文