gpt4 book ai didi

android - 使用 LinkedBlockingQueue 实现 IntentService?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:15 24 4
gpt4 key购买 nike

我正在尝试使用 IntentService 下载多个文件。 IntentService 一次按预期下载它们,唯一的问题是当 Internet 出现故障时,intent 服务不会停止下载,而是会卡在当前线程上。如果我设法停止当前线程,它将继续运行存储在其队列中的其他线程,即使互联网连接已断开也是如此。

在另一篇文章中有人建议我使用 LinkedBlockingQueue 并创建我自己的 Worker 线程,该线程会不断检查此队列中是否有新线程。现在我知道在创建和销毁线程时会增加一些开销,从而导致性能问题,但这对我来说不是问题。

在这一点上,我想做的就是了解 IntentService 是如何工作的,但我还没有(我已经看过代码),然后使用由 Worker 控制的 LinkedBlockingQueue 提出我自己的实现线。有没有人这样做过?可以提供一个工作示例,如果您对提供源代码感到不自在,伪代码对我来说很好。谢谢!

更新:我最终使用一个线程实现了我自己的 Intent 服务,该线程具有一个检查队列的循环器,该队列又存储从 startService(intent) 传递的 Intent 。

public class MyIntentService extends Service {



private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>();


public MyIntentService(){
super();
}



@Override
public void onCreate() {

super.onCreate();

new Thread(queueController).start();

Log.e("onCreate","onCreate is running again");


}



boolean killed = false;
Runnable queueController = new Runnable() {
public void run() {
while (true) {
try {
Download d =queue.take();

if (killed) {
break;
}
else {
d.downloadFile();
Log.e("QueueInfo","queue size: " + queue.size());
}
}
catch (InterruptedException e) {
break;
}

}
Log.e("queueController", "queueController has finished processing");
Log.e("QueueInfo","queue size: " + queue.toString());
}
};

class Download {
String name;
//Download files process
void downloadFile() {
//Download code here
}

Log.e("Download","Download being processed is: " + name);
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
}




public void killService(){
killed = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Download d = new Download();
d.setName(intent.getStringExtra("VIDEOS"));
queue.add(d);
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.e("stopSelf","stopSelf has been just called to stop the Service");
stopSelf();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}


}

我不太确定 onStartCommand() 方法中的 START_NOT_STICKY。如果它是返回或不返回的正确标志。对此的任何澄清将不胜感激!

最佳答案

更新:我最终使用一个线程实现了我自己的 Intent 服务,该线程具有一个循环程序,该循环程序检查队列,该队列又存储从 startService(intent) 传递的 Intent 。

公共(public)类 MyIntentService 扩展服务{

private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>();


public MyIntentService(){
super();
}



@Override
public void onCreate() {

super.onCreate();

new Thread(queueController).start();

Log.e("onCreate","onCreate is running again");


}



boolean killed = false;
Runnable queueController = new Runnable() {
public void run() {
while (true) {
try {
Download d =queue.take();

if (killed) {
break;
}
else {
d.downloadFile();
Log.e("QueueInfo","queue size: " + queue.size());
}
}
catch (InterruptedException e) {
break;
}

}
Log.e("queueController", "queueController has finished processing");
Log.e("QueueInfo","queue size: " + queue.toString());
}
};

class Download {
String name;
//Download files process
void downloadFile() {
//Download code here
}

Log.e("Download","Download being processed is: " + name);
}
public void setName(String n){
name = n;
}

关于android - 使用 LinkedBlockingQueue 实现 IntentService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com