RemoteViewsService is not running,

I want to make a list widget inside, I need to implement a class that extends RemoteViewsService, but this class never executes, my code is as follows:

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

Widget service

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

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

Change I found an error, I should use:

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

instead

 views.setRemoteAdapter(appWidgetId, updateIntent); 
+6
source share
2 answers

See RemoteViews.html # setRemoteAdapter

The SetRemoteAdapter function with 3 parameters is deprecated at API level 14+, and even if you name 3 parameters one, the id widget parameter is ignored. Thus, a function always receives only 2 valid parameters, i.e. view_id and intent.

This function basically says that I intend to use the intent as the data source for the view (specified by view_id).

+1
source

it works for me

  @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for (int widgetId : appWidgetIds) { RemoteViews mView = initViews(context, appWidgetManager, widgetId); appWidgetManager.updateAppWidget(widgetId, mView); } super.onUpdate(context, appWidgetManager, appWidgetIds); } @SuppressWarnings("deprecation") @SuppressLint("NewApi") private RemoteViews initViews(Context context, AppWidgetManager widgetManager, int widgetId) { RemoteViews mView = new RemoteViews(context.getPackageName(), R.layout.widget_layout); Intent intent = new Intent(context, WidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); mView.setRemoteAdapter(widgetId, R.id.widgetCollectionList, intent); return mView; } 
0
source

Source: https://habr.com/ru/post/944757/


All Articles