我是 android 的菜鸟,我正在尝试创建一个显示标题和片段 30 秒的新闻小部件。然后更改为另一个标题和片段。我希望小部件在连续循环中执行此操作。到目前为止,我只能显示第一个标题和片段。如何更改文本而不每 30 秒调用一次 alarmManager?任何帮助是极大的赞赏。
我的代码
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
String tickerheadline="testing";
String tickerstory="testing";
for (int i = 0; i < RssReader.rssheadline.size(); i++) {
tickerheadline = RssReader.rssheadline.get(RssReader.rssheadline.size()-(i+1));
tickerstory = RssReader.rssstory.get(RssReader.rssheadline.size()-(i+1));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {}
}, 30000);
}
updateViews.setTextViewText(R.id.headline, tickerheadline );
updateViews.setTextViewText(R.id.story, tickerstory );
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
编辑
public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
final RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
new Thread(new Runnable() {
public void run() {
tickerheadline="testing";
tickerstory="testing";
for (int i = 0; i < RssReader.rssheadline.size(); i++) {
tickerheadline = RssReader.rssheadline.get(RssReader.rssheadline.size()-(i+1));
tickerstory = RssReader.rssstory.get(RssReader.rssheadline.size()-(i+1));
context.runOnUiThread(action); //action cannot be resolved as variable
SystemClock.sleep(30000);
}
}
}).start();
Runnable action = new Runnable() {
public void run() {
updateViews.setTextViewText(R.id.headline, tickerheadline );
updateViews.setTextViewText(R.id.story, tickerstory );
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
};
} }