Android 意图服务阻塞UI
我需要在不阻塞用户UI的情况下执行回溯作业,因此我创建了一个test IntentService来检查它的工作方式,但此IntentServices会阻塞UI。Android 意图服务阻塞UI,android,intentservice,Android,Intentservice,我需要在不阻塞用户UI的情况下执行回溯作业,因此我创建了一个test IntentService来检查它的工作方式,但此IntentServices会阻塞UI。 以下是我在启动应用程序ic启动服务上的代码: Intent msgIntent = new Intent(this, AutoExchange.class); startService(msgIntent); AutoExchange.java public class AutoExchange extends Int
以下是我在启动应用程序ic启动服务上的代码:
Intent msgIntent = new Intent(this, AutoExchange.class);
startService(msgIntent);
AutoExchange.java
public class AutoExchange extends IntentService{
public AutoExchange() {
super("AutoExchange");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
问题是这个服务阻塞了主Ui,我做错了什么?尝试在Try-catch块周围创建一个新的Runnable()
警告:服务在其宿主进程的主线程中运行;除非您另行指定,否则服务不会创建自己的线程,也不会在单独的进程中运行
在android清单中注册您的服务。代替thread.sleep(),您可以使用 这里有一个例子
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// do stuff
}
},3000);
IntentService
不会阻止UI线程,因为它在工作线程上执行。你确定你在调用startService()
之后没有做一些阻塞UI线程的事情吗?不,我在create i startService上创建了一个空应用程序,仅此而已,在startService中,当一切正常时,您可以在正常服务中使用asyncTask。@АСаааааааааааааааааааааа1072,Thread.sleep将阻止当前线程,并且IntentService中正在运行的线程是BackgroundJobService
这是错误的,如果您没有在Android清单中注册您的服务,Android将不会运行您的服务,即使您调用startService