1

我正在尝试在主屏幕上显示一些新闻,并且我有此代码。我想在距离数组 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);

                }

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

我该如何实施?十分感谢


我实现了服务,但仍然抛出 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);

            }

    }    

我怎样才能解决这个问题?谢谢!

4

2 回答 2

0

如果 Activity 被阻止超过 5 秒,Android 会抛出一个没有响应的 Activity看到这个,这可能就是你不能响应的原因。为此使用不同的线程Handler或 asyncTask 。

于 2012-06-23T17:09:05.563 回答
0

使用服务来做网络的东西和解析。

请参阅Android 文档或此示例

编辑: 我认为在这种情况下使用IntentService并覆盖可能是个好主意onHandleIntent()

于 2012-06-23T17:09:58.893 回答