gpt4 book ai didi

android - 如何在小部件中循环文本?

转载 作者:行者123 更新时间:2023-11-30 03:53:21 24 4
gpt4 key购买 nike

我是 android 的新手,我正在尝试创建一个新闻小部件,显示标题和摘要 30 秒。然后更改为另一个标题和摘要。我希望小部件连续循环执行此操作。到目前为止,我只能显示第一个标题和 fragment 。如何在不每 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);
}
};

} }

最佳答案

无论您在方法中传递什么参数,您都可以创建类的构造函数并将其传递,并将“MainActivity.this”替换为“context”。不过,如果您有任何疑问,请告诉我...:)

public void updateAppWidget(){
new YourNewClass().start();
}
private class YourNewClass extends Thread {
@Override
public void run() {
for(--,--,--){
MainActivity.this.runOnUiThread(runnable);
SystemClock.sleep(30000);
}
}
}
private Runnable runnable = new Runnable() {
public void run() {

}
};

关于android - 如何在小部件中循环文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13771093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com