【问题标题】:Updating appwidget cause set values to default更新 appwidget 导致将值设置为默认值
【发布时间】:2016-06-08 14:40:23
【问题描述】:

我正在编写一个应用程序小部件,该小部件从服务器获取数据并将它们显示在 appwidget 中。 问题是当没有互联网连接并且此时系统更新小部件时,TextView 文本值重置为使用android:text="sometext" 设置的默认文本

事情是这样发生的:

  1. 小部件放置在主屏幕上
  2. Internet 连接处于活动状态
  3. 小部件已成功更新
  4. 来自服务器的响应文本安装在TextView
  5. 互联网连接无效
  6. 系统更新小部件
  7. TextView 中的先前文本重置为android:text="" 中设置的值

我知道我在某处做错了什么,因为在没有连接到 Internet 的其他小部件(不是我的)中不会重置。

文件WidgetProvider.java

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));
    }
}

文件 UpdatingService.java

public class UpdatingService extends IntentService {

    public static String LOG_TAG = "MYAPPLOG";

    public UpdatingService() {
        super("UpdatingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // getting widgetID from intent and other vars

        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);
                        // get data from server
                        // ...
                        // set values to the views
                        remoteViews.setTextViewText(R.id.textView, someText);

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d(LOG_TAG, "JSONObject failed");
                    }
                }
                else
                {
                    // LOG: error connection to server
                }
            }
            else
            {
                // LOG: No internet connection
            }

        // updating apwidget (set click action for the some button)
        // if not do update then button will not work

        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() {
        // using HttpURLConnection
    }
}

希望能得到您的帮助或小费。我写了几个小部件,都遇到了这个问题。非常感谢您的关注。

【问题讨论】:

    标签: java android service widget android-widget


    【解决方案1】:

    当发生 AppWidget 更新时,必须设置每个属性,例如文本视图的所有值、所有单击侦听器、颜色等。简单地说就是一切。未设置的将使用布局 XML 中的默认值。

    在您的情况下,您在 else 分支(“无互联网”)中什么都不做,因此您的应用小部件以默认文本结束。因此,当您从服务器获取数据时,您必须将其保存并在下次没有互联网连接时使用。

    【讨论】:

    • 好的,我在启动将属性设置为 textview 的服务代码之前粘贴了updateWidget(Context context, int widgetID),但它仍然无法正常工作。每次系统在没有互联网连接的情况下更新小部件时,它都会从 xml 设置值,即使这些值是由服务在之前的时间设置的。
    • 您正在服务中创建应用小部件的 UI,即 RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.initial_layout);。那是“重置” UI 的代码。所以不要更新您的updateWidget(Context context, int widgetID) 方法,而是用// LOG: No internet connection 更新Service 中的ELSE 分支。在那里,您将应用小部件的文本设置为您保存的值。
    • 我可能只需要在服务中创建一次 UI (RemoteViews remoteViews = new RemoteViews) 并且只能使用此 remoteViews(当我需要 remoteViews 时不要创建 avery)?跨度>
    • 当系统/启动器终止您的应用程序小部件的进程(无论出于何种原因)然后需要重新创建它时,这将不起作用。将someText 的值保存在SharedPreferences 或某处,然后将其加载到“no Internet” else 分支中。不要破解系统,只需始终设置所有必须设置的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多