2

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

问题是文本没有更新!

4

1 回答 1

3

我认为您错过了 onPostExecute() 中的字符串,例如:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
appWidgetManager.updateAppWidget(allWidgetIds, rv);

当您调用 appWidgetManager.updateAppWidget(...) 时会发生更新,但是当您只调用 rv.setTextViewText(R.id.ip, html); - 它没有。

希望它会有所帮助。

于 2013-03-22T13:32:51.190 回答