Android应用小工具 - 在更新的AsyncTask API调用

问题描述 投票:7回答:1

我想就我的Android主屏幕一个简单的小工具在那里我能填补我的邮递区号或城市,并通过提交这些数据,我希望能够从一个API调用,Openweathermap.org数据更新的Widget。

我所做的每一步,使其工作,但由于某种原因Widget的TextView中不会收集到的数据进行更新。

这是我的主要活动。

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

在onPostExecute中的AsyncTask看,我登录的结果。这有正确的数据。所以,寄托都来一直很好,但是当我想更新与setTextViewText的观点,没有任何反应。我是新来的Android的发展,我的想法。

有人在乎赐教?

java android android-asynctask android-widget
1个回答
0
投票

当你调用API和使用:

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

您需要使用AppWidgetManager更新UI。

下面是一个例子:

AppWidgetManager manager=AppWidgetManager.getInstance(context);
manager.updateAppWidget(widgetID,views);
© www.soinside.com 2019 - 2024. All rights reserved.