1

我是 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

4 回答 4

1

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

    }
};
于 2012-12-07T21:43:52.743 回答
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 )

于 2012-12-17T14:34:21.680 回答
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);
        }
    };

} 
于 2012-12-07T22:13:44.863 回答
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.
}

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

于 2012-12-07T22:08:34.137 回答