我想要什么:当用户添加小部件时,警报管理器开始更新添加的小部件。小部件从互联网(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);
}
}
问题是文本没有更新!