0

I am creating a widget that needs to make a network connection to update its information.

I'm pretty much using Android Studio boilerplate template for Widgets, and its onUpdate method comes with this:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
    }
}

where updateAppWidget is a static function.

The problem is that I'm making use of AsyncTask to make a network connection, retrieve some data and update the widget's its values:

private class APIQuery extends AsyncTask<Connection, Integer, String> {

    @Override
    protected String doInBackground(Connection... params) {
     // ...
    }
}

The problem is inside updateAppWidget because if I try to instantiate APIQuery from within updateAppWidget, Android Studio says that I cannot call a non-static method from a static method.

Based on this answer:

calling non-static method in static method in Java

The only way to call a non-static method is by instantiating the class, but if I instantiate APIQuery, then Android says that I have to either make updateAppWidget non-static, or APIQuery static.

What is the right way to approach this situation? I'm new to Java and Android development, and learning as I go.

Community
  • 1
  • 1
ILikeTacos
  • 17,464
  • 20
  • 58
  • 88

1 Answers1

0

You can try updating the widget either from the onPostExecute method of the async task, or you may send signal using broadcast receivers to the hosting activity to update the widget

J.Vassallo
  • 2,282
  • 3
  • 15
  • 14