I am writing an application widget that takes data from the server and displays it in the widget on the Android user’s working screen. The problem is that when the Internet connection is not available and at this point the system updates the widget, the text you set to TextView elements is reset to the default text set in the Android Studio visual editor.
Those. it happens like this:
- Install the widget
- Internet connection is
- Widget successfully updated
- Text from server response is set to
TextView - No internet connection
- The system updates the widget
- The previous text in the
TextViewreset to the value set inandroid:text=""
I know that somewhere I am doing something wrong, because other widgets do not reset anything when there is no connection to the Internet.
WidgetProvider.java file
public class WidgetProvider extends AppWidgetProvider { public static String LOG_TAG = "MYAPPLOG"; @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); Log.d(LOG_TAG, "onReceive"); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.d(LOG_TAG, "onUpdate"); for (int widgetID : appWidgetIds) { updateWidget(context, widgetID); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); Log.d(LOG_TAG, "onDeleted"); } public void updateWidget(Context context, int widgetID) { context.startService(new Intent(context, UpdatingService.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID)); } } File UpdatingService.java
public class UpdatingService extends IntentService { public static String LOG_TAG = "MYAPPLOG"; public UpdatingService() { super("UpdatingService"); } @Override protected void onHandleIntent(Intent intent) { // здесь получаем widgetID из intent и др. переменные RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.initial_layout); if(isConnected(getApplicationContext())) { String response = getServerResponse(); if(response != null) { try { JSONObject JSON = new JSONObject(response); // берем данные из ответа сервера // устанавливаем во вьюхи remoteViews.setTextViewText(R.id.textView, someText); } catch (JSONException e) { e.printStackTrace(); Log.d(LOG_TAG, "JSONObject failed"); } } else { // LOG: Ошибка соединения с сервером } } else { // LOG: Нет соединения с интернет } // обновляем виджет (устанавливаем клик по виджету) // если не обновить, то кнопка на виджете не будет работать Intent someIntent = new Intent(getApplicationContext(), WidgetProvider.class); someIntent.setAction(WidgetProvider.ACTION_GOTO); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), widgetID, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.goToLayout, pendingIntent); AppWidgetManager.getInstance(getApplicationContext().getApplicationContext()) .updateAppWidget(widgetID, remoteViews); } public boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.isConnected()) { return true; } return false; } public String getServerResponse() { // используем HttpURLConnection } } I really hope for your help or a little hint. I have already written several widgets and all with this sore. Thank you very much for your attention.