0

我正在尝试从具有待处理意图的 AppWidget 启动 MainActivity,但我无法弄清楚为什么它不会触发意图。该小部件有一个最多包含 3 个项目的 ListView,它绑定到 RemoteViewsService。我想在单击任何行时启动我的 MainActivity。谁能告诉我我可能做错了什么?

我已经检查了一些相关的帖子,比如Launching an activity from an AppWidget,甚至尝试从这里运行示例https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget,虽然示例有效,仍然无法说出我做了什么不同的事情。

以下是我的一些相关代码,如果我应该发布更多内容,请告诉我,如果我的帖子看起来不正确,请提前道歉,因为这是我的第一个问题:

小部件布局:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/stocks"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
/>

AppWidgetProvider 类:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(LOGTAG, "onUpdate");
        Log.d(LOGTAG, "appWidgetIds.length="+appWidgetIds.length);
        for (int i = 0; i < appWidgetIds.length; ++i) {
            Log.d(LOGTAG, "appWidgetIds[i]="+appWidgetIds[i]);
        }
        // update each of the app widgets with the remote adapter
        for (int i = 0; i < appWidgetIds.length; ++i) {
            updateWidget(context, appWidgetManager, appWidgetIds[i]);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        Log.d(LOGTAG, "updateWidget id="+appWidgetId);

        // Sets up the intent that points to the StackViewService that will
        // provide the views for this collection.
        Intent intent = new Intent(context, StockAppWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        // When intents are compared, the extras are ignored, so we need to embed the extras
        // into the data so that the extras will not be ignored.
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.stock_appwidget);
        rv.setRemoteAdapter(R.id.stocks, intent);

        Intent contentIntent = new Intent(context, MainActivity.class);
        //contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent startAppPendingIntent = 
                PendingIntent.getActivity(context, 0, contentIntent, 0);
        rv.setPendingIntentTemplate(R.id.stocks, startAppPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="x40241.chris.yuan.a4.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SearchableActivity" >
            <intent-filter>
                <action android:name="x40241.chris.yuan.a4.app.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service android:name=".StockServiceImpl" />
        <service android:name=".StockAppWidgetService"
                 android:permission="android.permission.BIND_REMOTEVIEWS" />
        <receiver android:name=".ServiceLauncher">  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED" />  
            </intent-filter>  
        </receiver>
        <receiver android:name=".StockAppWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/stock_appwidget_info" />
        </receiver>
    </application>

</manifest>
4

1 回答 1

1

我想我找到了答案,或者至少在相关帖子中找到了一个可行的解决方案:

应用小部件中的 AdapterViewFlipper:setPendingIntentTemplate() 和 setOnClickFillInIntent() 不起作用

事实证明我没有正确设置 FillInIntent。我应该在列表项的顶层视图而不是列表本身上调用 setOnClickFillInIntent。这是使它在 RemoteViewsFactory 中工作的代码更改:

    public RemoteViews getViewAt(int position) {
        if (DEBUG) Log.d(LOGTAG, "StockRemoteViewsFactory getViewAt position="+position);

        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.text_symbol, mStockItems.get(position).getSymbol());
        rv.setTextViewText(R.id.text_price, String.format("$%.2f", mStockItems.get(position).getPrice()));

        Intent fillInIntent = new Intent(Intent.ACTION_MAIN);
        fillInIntent.putExtra(StockAppWidgetProvider.ITEM_NUM, position);

        // set on the top level view of the list item, instead of the list view itself
        //rv.setOnClickFillInIntent(R.id.stocks, fillInIntent);
        rv.setOnClickFillInIntent(R.id.general_layout, fillInIntent);    

        return rv;
    }
于 2012-12-12T06:59:57.883 回答