Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RemoteViewsService doesn't execute,

Tags:

android

widget

I want to do a widget with a list inside, I have to implement a class that extends of RemoteViewsService, but this class never execute, my code is the following:

MyWidgetProvider

public class MyWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

    Intent updateIntent = new Intent(context, WidgetService.class);
    for (int appWidgetId : appWidgetIds) {
        updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
            views.setRemoteAdapter(appWidgetId, updateIntent);
        else
            views.setRemoteAdapter(appWidgetId, R.id.events_list, updateIntent);

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.events_list);         
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}

WidgetService

public class WidgetService extends RemoteViewsService {
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new WidgetFactory(getApplicationContext(), intent);
    }
}

And the Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.widget.example"
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" >    

    <receiver android:name=".MyWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info" /> 
    </receiver>

    <service android:name=".WidgetService"
        android:permission="android.permission.BIND_REMOTEVIEWS"
        android:exported="false">
    </service>

</application>

Edit; I have found the error, i have to use:

views.setRemoteAdapter(R.id.events_list, updateIntent);

instead of

views.setRemoteAdapter(appWidgetId, updateIntent);
like image 251
aloj Avatar asked May 11 '13 09:05

aloj


1 Answers

See RemoteViews.html#setRemoteAdapter

The SetRemoteAdapter function with 3 parameters is deprecated in API level 14+, and even if you called the 3 parameters one, the widget id parameter is ignores. So the function always receive only 2 valid parameters, i.e. view_id and intent.

This function basically says I'm going to use the intent as a data source for the view (indicated by view_id).

like image 76
xdu Avatar answered Sep 17 '22 04:09

xdu