在 Android 上从源轮询并更新 UI 的有效方法是什么?

发布于 2024-12-03 07:57:46 字数 1342 浏览 0 评论 0原文

我试图定期轮询给定网站以获取信息,然后依次更新 Android 应用程序中的 UI。

我已经设置了类似于以下的内容,但看起来很麻烦。我想我的两个具体问题是:

  1. view.post(new Runnable()); 是否必要,或者广播是否在 UI 线程上处理(允许直接执行 AsyncTask,因为必须从 UI 完成吗?

  2. 除了设置警报和接收 Intents 之外,还有什么方法可以定期启动 AsyncTask (或类似的东西)吗?

这是我正在运行的简化版本:

public class WebUpdates extends BroadcastReceiver
{

private AlarmManager am ;
private PendingIntent pi ;
private View view ; 
private Context listener ; 

public WebUpdates(Context listener, View view)
{
    this.listener= listener; 
    this.view = view ;

    am = (AlarmManager)listener.getSystemService(Context.ALARM_SERVICE) ;
    pi = PendingIntent.getBroadcast((Context)listener, 0, new Intent("WEBUPDATE"), 0) ;

    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, (long)15000, pi) ;
}

@Override
public void onReceive(Context context, Intent intent) 
{
    view.post(new Runnable() {
        public void run()
        {
            new WebUpdateTask().execute() ;
        }
    }) ;
}

private class WebUpdateTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... v) 
    {
        // bunch of web stuff... 
    }

    public void onPostExecute(Void v)
    {
        view.doBunchOfUpdates() ;
    }
}
}

这是我的第一个问题,因此我对任何格式问题表示歉意。任何意见或帮助将不胜感激!

I am trying to poll a given website periodically for information and then in turn update the UI in an Android app.

I have set something up similar to the following, but it just seems cumbersome. I guess my two specific questions would be:

  1. Is the view.post(new Runnable()); necessary or are the broadcasts handled on the UI thread (allowing the AsyncTask to be executed directly since it must be done from the UI)?

  2. Is there any way to periodically start an AsyncTask (or something comparable) besides setting up the alarms and receiving the Intents?

Here is a simplified version of what I am running:

public class WebUpdates extends BroadcastReceiver
{

private AlarmManager am ;
private PendingIntent pi ;
private View view ; 
private Context listener ; 

public WebUpdates(Context listener, View view)
{
    this.listener= listener; 
    this.view = view ;

    am = (AlarmManager)listener.getSystemService(Context.ALARM_SERVICE) ;
    pi = PendingIntent.getBroadcast((Context)listener, 0, new Intent("WEBUPDATE"), 0) ;

    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, (long)15000, pi) ;
}

@Override
public void onReceive(Context context, Intent intent) 
{
    view.post(new Runnable() {
        public void run()
        {
            new WebUpdateTask().execute() ;
        }
    }) ;
}

private class WebUpdateTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... v) 
    {
        // bunch of web stuff... 
    }

    public void onPostExecute(Void v)
    {
        view.doBunchOfUpdates() ;
    }
}
}

This is my first question, so I apologize for for any formatting issues. Any input or help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2024-12-10 07:57:46
  1. 根据 文档,onReceive() 在主线程上执行。所以你应该能够直接启动AsyncTask。
  2. 另一种选择是使用 TimerTimerTask。网上有很多示例,这里是一个。如果即使您的 Activity 未处于活动状态也需要接收 Intent,则 AlarmManager 会更好。如果您只需要 Activity 运行时的周期性行为,Timer 和 TimerTask 可能会更好。哦,我猜 ScheduledThreadPoolExecutor 是新代码的首选;我过去刚刚使用过 Timer/TimerTask。

但我不认为使用 AlarmManager 有什么坏处,即使你只需要在你的应用程序正在使用时更新,只要你在 onPause() 中取消闹钟即可。

  1. According to the documentation, onReceive() exceutes on the main thread. So you ought to be able to start the AsyncTask directly.
  2. Another option would be to use Timer and TimerTask. There are a number of examples on the web, here is one. AlarmManager is better if you need to receive the Intent even when your Activity is not active. Timer and TimerTask might be better if you only need the periodic behavior while your Activity is running. Oh and I guess ScheduledThreadPoolExecutor is preferred for new code; I've just used Timer/TimerTask in the past.

But I don't think there is any harm in using AlarmManager even if you only need to update when your app is being used, as long as you cancel the alarm in onPause().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文