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

标签 android android-activity widget android-intent android-pendingintent

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

    <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,每个都在自己的包和 list 中

最佳答案

我想你需要一个 <data>在你的 <intent-filter> 中标记为了使您触发的 Intent 与您注册的 Intent 过滤器相匹配。

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 应该是这样的:

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

关于Android:向 Intent 添加数据无法加载 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618630/

相关文章:

android - 当我单击“ ListView 内容”时,它们是通过不在我的应用程序中的 Web 浏览器打开的

android - 通过 Wireshark 实时捕获 Android 蓝牙流量

java - 从 Android 设备删除内存中的文件?

android - Flutter SetState 不更新文本

flutter - 信息:此函数的返回类型为 'Widget',但不以return语句结尾

java - 无法在 "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/jarsigner"找到可执行文件

android - 发送短信时接收广播

android - 如何在抽屉导航中添加 Tabhost

javascript - Web 小部件的 Twitter OAuth 回调问题

android - 在 Android 上使用 SubCut (Scala DI)