13

I have an app widget and the click event can be received in onUpdate() in my provider.
However, when I try to force close the home launcher, the click event is lost.
I even put breakpoints in all onEnabled(), onReceived()...etc: the connection seems to be lost.

As a result, how can I "re-connect" the button event?

WidgetProvider extends AppWidgetProvider:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate()");
        Log.d(TAG, "isLoading: " + CONSTANT.isLoading);
        // update each of the widgets with the remote adapter from updateService
        // Get all ids
        ComponentName thisWidget = new ComponentName(context, ScoreWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent);

//      super.onUpdate(context, appWidgetManager, appWidgetIds);
    }  

UpdateWidgetService extends Service:

@Override
        public void onStart(Intent intent, int startId) {

            Log.i(TAG, "Called");

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

            int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

            for (int i = 0; i < appWidgetIds.length; ++i) {
                // Here we setup the intent which points to the StackViewService which will
                // provide the views for this collection.
                Intent remoteViewsIntent = new Intent(this.getApplicationContext(), ScoreWidgetRemoteViewsService.class);
                remoteViewsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                // 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.
                remoteViewsIntent.setData(Uri.parse(remoteViewsIntent.toUri(Intent.URI_INTENT_SCHEME)));
                RemoteViews rv = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
                rv.setRemoteAdapter(appWidgetIds[i], R.id.score_list, remoteViewsIntent);

                // Set the empty view to be displayed if the collection is empty.  It must be a sibling
                // view of the collection view.
                rv.setEmptyView(R.id.score_list, R.id.empty_view);

                // Bind the click intent for the refresh button on the widget
                final Intent refreshIntent = new Intent(this.getApplicationContext(), ScoreWidgetProvider.class);
                refreshIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                refreshIntent.setAction(ScoreWidgetProvider.REFRESH_ACTION);
                final PendingIntent refreshPendingIntent = PendingIntent
                        .getBroadcast(this.getApplicationContext(), 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                rv.setOnClickPendingIntent(R.id.btn_refresh, refreshPendingIntent);

                appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
            }

    //        stopSelf();

            super.onStart(intent, startId);
        }
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
jjLin
  • 3,281
  • 8
  • 32
  • 55
  • Start a service OnUpdate ! you can do your works On service – Sree Reddy Menon Feb 23 '16 at 14:12
  • 1
    http://stackoverflow.com/questions/26108355/how-to-start-an-activity-by-the-click-on-an-image-in-widget/26288400#26288400 – Sree Reddy Menon Feb 23 '16 at 14:13
  • my widget contains listview, so it already have class that extends RemoteViewsService. so i need to write another on service? – jjLin Feb 23 '16 at 14:35
  • from my edited code, i tried to start a service. But when i force close launcher, the refresh button stills not work. – jjLin Feb 25 '16 at 14:27
  • what refreshbutton does? what do you mean by "for close launcher" - app is removing from background apps list? – Sree Reddy Menon Feb 25 '16 at 15:49
  • refresh button is to update the listview in widget. – jjLin Feb 26 '16 at 00:35
  • somethime, home launcher will restart when low memory. in this case, i go to app manager and stop the launcher to pretend this situation. – jjLin Feb 26 '16 at 00:36
  • Did you notice that with every home launcher (included stock one)? – JJ86 Mar 03 '16 at 10:09
  • Just tried this by restarting Nova launcher and it worked for my simple widget. What launcher are you testing on? – Jeroen Mols Mar 03 '16 at 12:33
  • 1
    I'm wondering if you need to add the appWidgetIds you would like to update to the pending intent to make it work. (In your case ALL appwidgetids, because you reuse the same pendingintent for every widget) https://stackoverflow.com/questions/3570146/is-it-possible-to-throw-an-intent-for-appwidget-update-programmatically – Jeroen Mols Mar 03 '16 at 12:40
  • i found that other widgets seems no problems after launcher restarts – jjLin Mar 04 '16 at 05:45

1 Answers1

0

Make sure that you are using the correct context in your onStart function. Check out getApplicationContext in the onStart part of your code, passing in the wrong type of context can cause errors. Here is a link for more information: Context.

Community
  • 1
  • 1
Nate
  • 21
  • 6