我想要的: 当用户添加小部件时,警报器开始更新添加的小部件。 小部件从互联网(json资源)获取数据。 小部件使用专用的json页面(每个小部件都有一个获取数据的页面:例如.test.json?page = 5)
我已经有一个扩展AppWidgetProvider的类。
在这个课程中有一个功能:private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
在这个函数里面有:
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
new HttpRequestTask(SELECTED_PAGE).execute(remoteViews);
这是HttpRequestTask类:
class HttpRequestTask extends AsyncTask<RemoteViews, Void, String> {
private RemoteViews rv;
private String url;
public HttpRequestTask(String string) {
url = string;
}
protected String doInBackground(RemoteViews... contextParam) {
String html = "";
try {
rv = contextParam[0];
HttpClient client = new DefaultHttpClient();
HttpGet request
= new HttpGet("http://www.hwlogos.com/"+url+".txt");
HttpResponse response;
response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return html;
}
protected void onPostExecute(String html) {
rv.setTextViewText(R.id.ip, html);
}
}
问题是文本没有更新!
答案 0 :(得分:3)
我认为你错过了onPostExecute()中的字符串,如:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
appWidgetManager.updateAppWidget(allWidgetIds, rv);
当您调用appWidgetManager.updateAppWidget(...)时会发生更新,但是当您只调用rv.setTextViewText(R.id.ip,html)时; - 它没有。
希望它会有所帮助。