java - Android App Widget - 更新中的 AsyncTask API 调用

标签 java android android-asynctask android-widget

我想在我的 Android 主屏幕上制作一个简单的小部件,我可以在其中填写我的邮政编码或城市,并且通过提交该数据,我希望能够使用来自 API 调用的数据更新小部件到 Openweathermap .org.

我已采取每一步使其正常工作,但出于某种原因,Widget textView 不会使用收集的数据进行更新。

这是我的主要 Activity 。

public class WeerManWidget extends AppWidgetProvider {

public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    CharSequence widgetText = WeerManWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weer_man_widget);
    views.setTextViewText(R.id.appwidget_text, widgetText);



    Intent configIntent = new Intent(context, WeerManWidgetConfigureActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, appWidgetId, configIntent, 0);
    views.setOnClickPendingIntent(R.id.btnSettings, configPendingIntent);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE + Integer.toString(appWidgetId));



    new GetWeatherTask(views).execute(widgetText.toString());



    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    // When the user deletes the widget, delete the preference associated with it.
    for (int appWidgetId : appWidgetIds) {
        WeerManWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
    }
}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
}
}

这是我用于 API 调用的类。

public class GetWeatherTask extends AsyncTask<String, String, String> {

private RemoteViews views;

GetWeatherTask(RemoteViews views) {
    this.views = views;
}

@Override
public String doInBackground(String... params) {

    String postalCode = params[0];

    HttpURLConnection urlConnection = null;
    URL url = null;
    JSONObject object = null;
    JSONArray myArray = null;
    InputStream inStream = null;

    try {
        url = new URL("http://api.openweathermap.org/data/2.5/weather?q="+postalCode+",nl&appid=XXXXX&units=metric");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();

        inStream = urlConnection.getInputStream();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
        String temp, response = "";
        while ((temp = bReader.readLine()) != null) {
            response += temp;
        }

        object = (JSONObject) new JSONTokener(response).nextValue();
        JSONObject obj = object.getJSONObject("main");
        String weatherTemp = obj.getString("temp");

        double weatherFloat = Double.parseDouble(weatherTemp);
        //weatherFloat = (weatherFloat - 273.15);

        String newTemp = String.valueOf(weatherFloat);

        return newTemp;
    } catch (Exception e) {

        return e.toString();
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException ignored) {

            }
        }
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

@Override
public void onPostExecute(String result) {
    Log.v("WeerManWidget", result);

    views.setTextViewText(R.id.appwidget_text, result);
}
}

在AsyncTask的onPostExecute中看到,我把结果记录下来。这有正确的数据。所以一切都很好,但是当我想用 setTextViewText 更新 View 时,没有任何反应。我是 Android 开发的新手,没有想法。

有没有人愿意开导我?

最佳答案

调用 API 并使用后:

views.setTextViewText(R.id.appwidget_text, result);

您需要使用 AppWidgetManager 来更新 UI。

这是一个例子:

AppWidgetManager manager=AppWidgetManager.getInstance(context);
manager.updateAppWidget(widgetID,views);

关于java - Android App Widget - 更新中的 AsyncTask API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641188/

相关文章:

java - 无法找到要存储在 ListView 中的所有音频文件

java - 设置 MockServletContext 的真实路径

android - android中的日历日/月/周 View

android - HistoryRecord 的 Activity 空闲超时

java - 在同一数据库上运行的不同线程中的多个游标

android - 使用 AsyncTask onPostExecute 链接到新 Activity ?

Java 到 JavaScript 数组的数组?

android - 无法在 android 中实现 GoogleApiClient.ServerAuthCodeCallbacks

android - Asynctask(从 alarmmanager 执行)回调到 ui

java - 在 Gae 上创建一个大的 Zip 文件(在 blobstore 中)