Widget setOnClickPendingIntent not starting service

社会主义新天地 提交于 2019-12-04 09:02:09

Well I have managed to fix it

public class WidgetProvider extends AppWidgetProvider {

@Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {

    Log.e("ERROR", "onUpdate method called");

    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch ExampleActivity
        Intent intent = new Intent(context, UpdateService.class);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
        Log("Pending Intent = " + pendingIntent.toString());

        // Get the layout for the App Widget and attach an on-click listener to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widge, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

}

public static class UpdateService extends Service {

    @Override
    public void onCreate() {
              .........
              .........
           }

           // Service stuff here
   }
}

I am not entirely sure what was wrong with doing it in onEnable. Maybe someone can shed some light as to why.

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