Angel Doza
Angel Doza

Reputation: 1134

Problems Implmenting Widget Android Studio - AsynTask

I'm new programming with Android Studio. In my MainActivity.java I call to ReadPage class, that extends from AsyncTask (to could read a web page in a JSON format) where I overrides doInBackground, onProgressUpdate and onPostExecute; until here, the app is working great.

But, I would like to implement a widget AppWidget.java in order to could read the same page in JSON format and print the given information in the widget fields. Due to ReadPage class is defined into MainActivity.java, I wrote a ReadPageWidget function that is almost the same function, but I can't implement it and let it working:

In AppWidget.java, I understand that in my onUpdate function I have to invocate new ReadPageWidget(views).execute("Updating") and then appWidgetManager.updateAppWidget(appWidgetId, views); where views was defined previously like RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);

In my widget I would like to have a button that on pressed, it updates the information from the web page, so I defined a function public void UpdateDate(View view).

I don't know how to implement this widget and, all the proobs that I do, finishes forcing the close of the application.

Edited: According to the basic example of creating widgets, I have defined the onUpdate function like this:

public class AppWidget extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
            SimpleDateFormat Date = new SimpleDateFormat("HH:mm:ss aa");
            String DateFormat = Date.format(Calendar.getInstance().getTime());
            views.setTextViewText(R.id.TVUpdatingDate, DateFormat);
            new ReadPageWidget(views).execute("Updating");
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
    ...
}

In this, I created the instance of ReadPageWidget and execute the AsynTask, using the parameter views. Only when I run the application, the field DateFormat is updated, in spite of I have defined android:updatePeriodMillis="3000" into my app_widget_info.xml

My app_widget.xml is defined like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Field"
        android:id="@+id/FieldJSON"
        android:textSize="12sp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="UpdatingDate"
        android:id="@+id/BUpdatingDate"
        android:onClick="UpdateDate"
        android:textSize="12sp"
        />
</RelativeLayout>

where FieldJSON should be updated by the widget with the ReadPageWidget function.

The only differences between ReadPage and ReadPageWidget are in definition of attributes and the constructor method:

public class ReadPage extends AsyncTask<String, Integer, String> {
    MainActivity Context;
    ProgressBar ProgressBarReadPage;
    TextView TVPage;

    public ReadPage(MainActivity Context) {
        this.Context = Context;
    }

    ...
}

vs

public class ReadPageWidget extends AsyncTask<String, Integer, String> {
    private RemoteViews views;

    public ReadPageWidget(RemoteViews views) {
        this.views = views;
    }
}

Edited:

I have added the onclick function in the button BUpdatingDate and UpdateDate function in AppWidget.java:

public void UpdateDate(View view) {
    Toast.makeText(view.getContext(), "Clicked!!", Toast.LENGTH_SHORT).show();
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(view.getContext());
    ComponentName thisAppWidgetComponentName = new ComponentName(view.getContext().getPackageName(), getClass().getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName);
    this.onUpdate(view.getContext(), appWidgetManager, appWidgetIds);
}

Upvotes: 0

Views: 69

Answers (1)

Bita Mirshafiee
Bita Mirshafiee

Reputation: 650

What I think is Happened here is because of not calling the onUpdate fun in a right place :

for updating UI you should put all ui related updating in onPostExecute() of AsyncTask

and calling apis and getting JSONs are in doInBackGround() of AsyncTask

Upvotes: 1

Related Questions