在 Android 上从源轮询并更新 UI 的有效方法是什么?
我试图定期轮询给定网站以获取信息,然后依次更新 Android 应用程序中的 UI。
我已经设置了类似于以下的内容,但看起来很麻烦。我想我的两个具体问题是:
view.post(new Runnable()); 是否必要,或者广播是否在 UI 线程上处理(允许直接执行 AsyncTask,因为必须从 UI 完成吗?
除了设置警报和接收 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:
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)?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但我不认为使用 AlarmManager 有什么坏处,即使你只需要在你的应用程序正在使用时更新,只要你在 onPause() 中取消闹钟即可。
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().