Search code examples
androidandroid-widgetclickcommonsware

Widget Click not Working?


I am trying to implement widget based on commonsware code 'LoremWidget', see here at Github

The problem is that the click is not working

on WidgetUpdate, same as WidgetProvider

for (int appWidgetId : appWidgetIds) 
{
 RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
 Intent configIntent = new Intent(context, WidgetService.class);
 PendingIntent configPendingIntent =PendingIntent.getActivity(context, 0, configIntent, 0);
 remoteView.setPendingIntentTemplate(R.id.imgNext, configPendingIntent);
 appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}

in the manifest, similarly as Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.widget"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <activity
         android:name=".WidgetConfig"
         android:label="@string/app_name" >
         <intent-filter>
              <action
                android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
         </intent-filter>
     </activity>
    <receiver
        android:name="BasicWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
    </receiver>
      <service android:name="WidgetService"
               android:permission="android.permission.BIND_REMOTEVIEWS" />
</application>
</manifest>

WidgetService, similar to WidgetService

public class WidgetService extends RemoteViewsService 
{
    private static final String TAG=WidgetService.class.getName();
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) 
    {
        Log.d(TAG, "inside remoteview");
        return(new WidgetFactory(this.getApplicationContext(),
                                     intent));
    }
}

WidgetFactory getView method as here

public RemoteViews getViewAt(int arg0) 
    {
        Log.d(TAG, "inside the getview at");
        RemoteViews row=new RemoteViews(ctxt.getPackageName(), R.layout.widget);
            row.setViewVisibility(R.id.imgNext, View.INVISIBLE);
        Intent i=new Intent();
        Bundle extras=new Bundle();
        return(row);
    }

Questions:

1. why is widget not updating, no logs are generated infact the only log relevant to my code is

05-15 23:24:27.648: I/ActivityManager(161): START {flg=0x10000000 cmp=com.demo.widget/.WidgetService bnds=[509,299][547,337]} from pid -1

2. Is this the right way to implement just a simple click in widget, is there any other way for it, is this widgetfactory the only way?


Solution

  • You might consider reading the book for these samples, specifically the chapter that covers this sample, as you would have found your answers there.

    why is widget not updating

    Because you are creating an activity PendingIntent, with an Intent that points to a service, from what I see:

     Intent configIntent = new Intent(context, WidgetService.class);
     PendingIntent configPendingIntent =PendingIntent.getActivity(context, 0, configIntent, 0);
     remoteView.setPendingIntentTemplate(R.id.imgNext, configPendingIntent);
    

    This does not match what the sample does.

    Is this the right way to implement just a simple click in widget, is there any other way for it, is this widgetfactory the only way?

    This is how you process clicks in an AdapterView used in an app widget.