Android Long-Service 创建 bg 线程,我不知道发生了什么
基本上我正在为Android开发一个RSS提要,并且我想要一个更新服务,对于每个源,在特定时间获取提要。
想象一下:
来源 1 - 5 分钟 来源2 - 10 分钟 Source3 - 10 分钟
我构建了一个差异列表,仅使用一个发出更新请求的线程。 数据结构保持如下:
Source1(下一个睡眠 - 5)、Source2(下一个睡眠 - 0)、Source 3(下一个睡眠 5).. 等等。
我所做的是,我总是添加到提供者列表中(即包含 id 和 URL),当 sleep != 0 时,我订购更新(使用异步任务,我传递要更新的提供程序列表,asynctask 为我完成这项工作,也就是说......转到互联网,解析内容,并更新数据库),
但存在一个工作线程,该线程正在休眠,只是为了将一组提供程序分组并发送到 AsyncTask 进行更新,
这是什么问题。 首先,线程在特定时间不休眠,线程必须休眠 10 分钟,我做了一个将分钟转换为 milis 的方法,即:
private static int MinutesToMilis(int minutes) { return minutes * 1000 * 60; }
我正在使用 alogcat,我手机上的一个小部件,线程休眠大约 25 分钟( WTF):|
我验证的另一件事是,当手机充电时,服务工作正常......
这是代码:
worker = new Thread(new Runnable()
{
@Override
public void run()
{
Provider[] enabledProviders = feedService.getAllProvidersEnabledData();
// Build a differential list from enabledProviders
DifList<Provider> list = getListForProviders(enabledProviders);
//
// Code starts here
int sleep;
DifNode<Provider> header = list.getFirst();
sleep = header.getInitial();
LinkedList<Provider> data = new LinkedList<Provider>();
Log.d(TAG, String.format("Worker thread started @ %s", now()));
try
{
for(int idx = 0 ; true ; list.reorderFirst(), header = list.getFirst(), idx++)
{
Log.d(TAG, String.format("Iteration %s with sleep %s", idx, sleep));
if(sleep > 0) {
Log.d(TAG, String.format("Next download in %s minutes", sleep));
// Suspend worker thread for sleep time
Thread.sleep(MinutesToMilis(sleep));
Log.d(TAG, "Worker thread waked up from sleep");
}
// Add to data
data.add(header.getKey());
// Set next sleep time
sleep = header.getDifference();
if(sleep != 0)
{
Log.d(TAG, "Worker preparing for update..");
//
// If difference to next is not 0 we dump data to async task
for(Provider p : data){
Log.d(TAG, String.format("Starting update %s @ %s", p.Name, now()));
}
Provider[] arrayData = new Provider[data.size()];
data.toArray(arrayData);
data.clear();
new UpdateBundleOfFeedsTask().execute(arrayData);
}
}
}
catch(InterruptedException ex)
{
Log.d(TAG, String.format("Worker thread was interrupted @ %s", now()));
isRunning = false;
}
Log.d(TAG, String.format("Worker thread exit @ %s", now()));
}
});
Basically i am developing a rss feed for android, and i want to have a updater service that, for each source, get the feeds in specific time.
Imagine:
Source1 - 5minutes
Source2 - 10minutes
Source3 - 10minutes
I build a differential list to use only one thread that make the requests for update.
The datastructure stays like this:
Source1 (next sleep - 5), Source2 (next sleep - 0, Source 3 (next sleep 5).. and so on..
What i do is, i add always to a list of providers (that contains the id and URL) and when sleep is != 0, i order the update (that uses async task, i pass the list of providers to update, and asynctask do the job for me, that is... go to internet, parse content, and update database)
but exists one worker thread that is sleeping just for the sake of group a bundle of providers and sent to AsyncTask to update.
What's the problem..
First the thread don't sleep for the specific time, the thread must sleep 10 minutes, i made a method for convert minutes to milis, that is:
private static int MinutesToMilis(int minutes) { return minutes * 1000 * 60; }
i am using alogcat, an widget on my phone and the thread sleeps about 25 minutes (WTF) :|
Other thing that i verified is that, when phone is charging, the service works OK...
Here is the code:
worker = new Thread(new Runnable()
{
@Override
public void run()
{
Provider[] enabledProviders = feedService.getAllProvidersEnabledData();
// Build a differential list from enabledProviders
DifList<Provider> list = getListForProviders(enabledProviders);
//
// Code starts here
int sleep;
DifNode<Provider> header = list.getFirst();
sleep = header.getInitial();
LinkedList<Provider> data = new LinkedList<Provider>();
Log.d(TAG, String.format("Worker thread started @ %s", now()));
try
{
for(int idx = 0 ; true ; list.reorderFirst(), header = list.getFirst(), idx++)
{
Log.d(TAG, String.format("Iteration %s with sleep %s", idx, sleep));
if(sleep > 0) {
Log.d(TAG, String.format("Next download in %s minutes", sleep));
// Suspend worker thread for sleep time
Thread.sleep(MinutesToMilis(sleep));
Log.d(TAG, "Worker thread waked up from sleep");
}
// Add to data
data.add(header.getKey());
// Set next sleep time
sleep = header.getDifference();
if(sleep != 0)
{
Log.d(TAG, "Worker preparing for update..");
//
// If difference to next is not 0 we dump data to async task
for(Provider p : data){
Log.d(TAG, String.format("Starting update %s @ %s", p.Name, now()));
}
Provider[] arrayData = new Provider[data.size()];
data.toArray(arrayData);
data.clear();
new UpdateBundleOfFeedsTask().execute(arrayData);
}
}
}
catch(InterruptedException ex)
{
Log.d(TAG, String.format("Worker thread was interrupted @ %s", now()));
isRunning = false;
}
Log.d(TAG, String.format("Worker thread exit @ %s", now()));
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的不应该这样做,即拥有一个永不消亡的线程。我强烈建议您查看警报: http://developer.android.com /reference/android/app/AlarmManager.html
一种好的方法是:
pendingIntent
,尽管在您的情况下它似乎有点太频繁了即每5-10分钟只阅读一些RSS。You should really not be doing it that way, ie having a thread that never dies. I strongly suggest you have a look at alarms : http://developer.android.com/reference/android/app/AlarmManager.html
A good way to go would be :
pendingIntent
for each client, although in your case its seems to be a little too frequent to do that each 5-10 minutes only to read some RSS.