Start MainActivity from AppWidget doesn't work

爷,独闯天下 提交于 2019-12-12 03:35:25

问题


I'm trying to launch my MainActivity from my AppWidget with pending intent but I can't figure out why it doesn't fire the intent. The widget has a ListView with up to 3 items, which is bind to a RemoteViewsService. I want to start my MainActivity when any of the rows is clicked. Can anyone tell me what I might done wrong?

I already checked some related posts like this Launching an activity from an AppWidget, and even tried to run the sample from here https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget, and while the sample works, still can't tell what I did differently.

Here are some of my code that are relevant, please let me know if I should post more, and apologize in advance if my post doesn't look right as this is my first question:

Widget layout:

<?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 class:

@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);
    }

manifest:

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

回答1:


I think I found the answer, or at least a workable solution in a related post:

AdapterViewFlipper in app widget: setPendingIntentTemplate() and setOnClickFillInIntent() not working

It turns out I wasn't setting the FillInIntent properly. I should call setOnClickFillInIntent on the top level view of the list item instead of the list itself. Here's the code change that made it work in 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;
    }


来源:https://stackoverflow.com/questions/13828747/start-mainactivity-from-appwidget-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!