Android:将数据添加到Intent无法加载Activity [英] Android: Adding data to Intent fails to load Activity

查看:54
本文介绍了Android:将数据添加到Intent无法加载Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小部件,当用户单击小部件主体时,该小部件应调用主应用程序的活动.我的设置适用于单个窗口小部件实例,但是对于同一窗口小部件的第二个实例,PendingIntent将被重用,结果,我作为额外实例发送的重要信息将被第一个实例覆盖.所以我想我应该将小部件ID作为Intent数据传递,但是,一旦我添加Intent#setData,我就会在日志中看到正确触发了2个单独的Intent,但是Activity无法拾取它,因此基本上不会出现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,分别位于其自己的包和清单中.

解决方案

我认为您需要在<intent-filter>中添加一个<data>标记,以便触发的意图与您已注册的意图过滤器相匹配./p>

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 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

解决方案

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>

这篇关于Android:将数据添加到Intent无法加载Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆