2

I am attaching a pending intent to a widget button via .setOnClickPendingIntent and getting no response from it when the button is clicked. Oddly enough, when I implement this same code in my download service, it wants to work (but won't because the service runs on a loop for multiple cycles so the activity just pops up and goes away again on a button click.)

I need this to be able to run on my main thread but can't for the life if me figure out why it's not calling as expected.

The listener is attached to the RemoteViews button in the method below:

public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId) {
    mContext = context;

    RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    // Create a PendingIntent to open the config activity
    Intent configIntent = new Intent(context, ConfigActivity.class);
    configIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

    // Keep in mind PendingIntent uniqueness rules. Use the widget ID as a request code
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, widgetId, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Attach the PendingIntent to our config button
    widgetViews.setOnClickPendingIntent(R.id.configButton, configPendingIntent);

    appWidgetManager.updateAppWidget(widgetId, widgetViews);

    if(loadChached() != null) {
        Weather weather = loadChached();

        widgetViews.setTextViewText(R.id.conditionsDisplay, weather.getConditions());
        widgetViews.setTextViewText(R.id.tempDisplay, String.valueOf(weather.getDisplayTemp()));
        widgetViews.setTextViewText(R.id.timestampDisplay, weather.getTimeStamp());

        if(loadPreferences(context).equals("Dark")) {
            Log.i(TAG, "updateWidget: Activating Dark Theme");
            widgetViews.setTextColor(R.id.conditionsDisplay, context.getResources().getColor(android.R.color.background_dark));
            widgetViews.setTextColor(R.id.tempDisplay, context.getResources().getColor(android.R.color.background_dark));
            widgetViews.setTextColor(R.id.timestampDisplay, context.getResources().getColor(android.R.color.background_dark));
        }

        else if(loadPreferences(context).equals("Light")) {
            Log.i(TAG, "updateWidget: Activating Light Theme");
            widgetViews.setTextColor(R.id.conditionsDisplay, context.getResources().getColor(android.R.color.white));
            widgetViews.setTextColor(R.id.tempDisplay, context.getResources().getColor(android.R.color.white));
            widgetViews.setTextColor(R.id.timestampDisplay, context.getResources().getColor(android.R.color.white));
        }

        appWidgetManager.updateAppWidget(widgetId, widgetViews);
    }

    Intent updateIntent = new Intent(context, DownloadIntentService.class);
    updateIntent.setAction(WeatherWidgetProvider.ACTION_UPDATE_WEATHER);

    Log.i(TAG, "onUpdate: Starting Service");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        try {
            context.startForegroundService(updateIntent);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }

    else {
        try {
            context.startService(updateIntent);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The activity I wish to launch is registered in the manifest with an intent filter:

<activity android:name=".ConfigActivity">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

Any assistance is greatly appreciated.

John Mack
  • 95
  • 1
  • 7
  • When you click the button do you see anything in the logcat? Don't filter the logcat as you might miss something important! – David Wasser Mar 15 '19 at 16:51
  • 1
    It wasn't being called. The problem was that I was calling .updateWidget from the service as well, overriding anything added to it in the provider. Once I moved all updates back to the main thread, the problem was resolved. – John Mack Mar 17 '19 at 02:38

1 Answers1

1

The problem was that I was calling .updateWidget from the service as well, overriding anything added to it in the provider. Once I moved all updates back to the main thread, the problem was resolved.

John Mack
  • 95
  • 1
  • 7
  • 1
    You can accept this answer by clicking the green checkmark next to the answer. That will get the question off the list of unanswered questions. – David Wasser Mar 18 '19 at 13:52