如何在窗口小部件中循环文本?

时间:2012-12-07 21:16:19

标签: android loops text timer android-widget

我是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);
        }
    };

} }

4 个答案:

答案 0 :(得分:1)

无论你在方法中传递什么args,你都可以创建一个类的构造函数并传递它,并将“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() {

    }
};

答案 1 :(得分:0)

fwiw,你提供的代码同时触发所有可运行的东西(从for循环开始起30秒)。

这是我通常使用的模式:

private Handler mHandler = new Handler();
private static final int NEWS_UPDATE_DELAY = 30*1000;

private Runnable mNewsUpdateHandler = new Runnable() {
     @Override 
     void run() 
     {
          updateNews();
          mHandler.postDelayed(mNewsUpdateHnadler, NEWS_UPDATE_DELAY);
     }
}

private void startNews() 
{
    stopNews();
    mNewsUpdateHandler.run();
}
private void stopNews()
{
    mHandler.removeCallback(mNewsUpdateHandler);    
}
private updateNews()
{
      // your stuff here.
}

如果还没有,您需要一个服务来运行该代码。无法通过活动进行连续的appwidget更新,因为当用户切换到另一个应用程序时,活动会消失..

答案 2 :(得分:0)

现在您的代码看起来像这样。希望它会帮助你...

public static void updateAppWidget(final Activity context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
    final RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);

    new Thread(new Runnable() {
        public void run() {
            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));

                context.runOnUiThread(action);
                SystemClock.sleep(30000);

//                  Handler handler = new Handler(); 
//                  handler.postDelayed(new Runnable() { 
//                       public void run() {} 
//                      }, 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);
        }
    };

} 

答案 3 :(得分:0)

不知道这是否是解决问题的方法。无论如何,action无法解析,因为它是在您想要访问它之后声明的。试着像这样声明:

public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
    final RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);

    final Runnable action = new Runnable() {
        public void run() {
            updateViews.setTextViewText(R.id.headline, tickerheadline);
            updateViews.setTextViewText(R.id.story, tickerstory);

            appWidgetManager.updateAppWidget(appWidgetId, updateViews);
        }
    };

    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();
}

此外,runOnUiThread是类Activity的方法,而不是Contexthttp://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable