我想在我的Android主屏幕上创建一个简单的小部件,我可以填写我的邮政编码或城市,并通过提交该数据,我希望能够使用API调用Openweathermap的数据更新Widget .ORG。
我已经尽一切努力使其发挥作用,但出于某种原因,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);
}
}
在AsyncTask中的onPostExecute中,我记录了结果。这有正确的数据。因此,everthing很顺利,但是当我想用setTextViewText更新视图时,没有任何反应。我是Android开发的新手,并且没有想法。
有人想开导我吗?
答案 0 :(得分:0)
调用API并使用后:
views.setTextViewText(R.id.appwidget_text, result);
您需要使用AppWidgetManager
来更新UI。
这里是一个例子:
AppWidgetManager manager=AppWidgetManager.getInstance(context);
manager.updateAppWidget(widgetID,views);