在主屏幕小部件中显示一个数组

时间:2012-06-23 16:47:29

标签: android arrays widget

我想在主屏幕上显示一些新闻,我有这个代码。我希望在从阵列中说出5秒之后显示每个新内容。

public class PointlessWidget extends AppWidgetProvider{

static final String DATA_TITLE = "T";
static final String DATA_LINK  = "L";
static LinkedList<HashMap<String, String>> data1;
static String feedUrl;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    feedUrl = "http://www.blabla.com";


            XMLParser parser = new XMLParser(feedUrl); 
                data1 = (LinkedList<HashMap<String, String>>)parser.parse();
                                Iterator<HashMap<String, String>> itr = data1.iterator();
                Object element;
                HashMap<String, String> entry;
                String Temp_URL=null,Temp_TITULO=null,Actual_URL,Actual_TITULO,Anterior_URL = null,Anterior_TITULO,Sigiente_URL,Sigiente_TITULO;

                while (itr.hasNext()) {
                    element = itr.next();
                    entry = (HashMap<String, String>) element;
                    mierda = entry.get("T");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    v.setTextViewText(R.id.word_title, mierda);
                    AppWidgetManager manager = AppWidgetManager.getInstance(context);
                    manager.updateAppWidget(appWidgetIds, v);
                    Log.i("***Selected Tab", "Im currently in tab with index::" + mierda);

                }

问题是我设法显示一个或两个元素,然后它停止,因为它说的对象在wait()之前没有被线程锁定的对象。

我该如何实现? 非常感谢你


我实现了服务,但仍然抛出了ANR错误。

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
        // To prevent any ANR timeouts, we perform the update in a service
        context.startService(new Intent(context, UpdateService.class));
    }

    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
            // Build the widget update for today
            RemoteViews v = new RemoteViews(this.getPackageName(), R.layout.widget_layout);
            feedUrl = "http://www.blabla.com";


                    XMLParser parser = new XMLParser(feedUrl); 
                        data1 = (LinkedList<HashMap<String, String>>)parser.parse();
                        int size = data1.size();
                        Iterator<HashMap<String, String>> itr = data1.iterator();
                        Object element;
                        HashMap<String, String> entry;
                        String Temp_URL=null,Temp_TITULO=null,Actual_URL,Actual_TITULO,Anterior_URL = null,Anterior_TITULO,Sigiente_URL,Sigiente_TITULO;

            while (itr.hasNext()) {
                element = itr.next();
                entry = (HashMap<String, String>) element;
                mierda = entry.get("T");

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                v.setTextViewText(R.id.word_title, mierda);
                ComponentName thisWidget = new ComponentName(this, PointlessWidget.class);
                AppWidgetManager manager = AppWidgetManager.getInstance(this);
                manager.updateAppWidget(thisWidget, v);
                Log.i("***Selected Tab", "Im currently in tab with index::" + mierda);

            }

    }    

我该如何解决这个问题? 谢谢!

2 个答案:

答案 0 :(得分:0)

如果活动被阻止超过5秒See this,Android会抛出一个没有响应的活动,这可能就是你不能做到的原因。为此使用不同的threadHandler或asyncTask。

答案 1 :(得分:0)

使用服务进行网络搜索和解析。

请参阅Android docs或此example

修改 我认为在这种情况下使用IntentService并覆盖onHandleIntent()

可能是一个好主意