我有两个有界服务,以及一些在需要时将自己绑定到服务的活动。
问题是当活动写出从显示器中的服务获得的信息时,应用程序的性能会很慢。
Android的文档说明了Stated Services:
警告:服务在与应用程序相同的进程中运行 声明它,并在该应用程序的主线程中,通过 默认。因此,如果您的服务执行密集或阻止操作 当用户与同一应用程序中的活动进行交互时, 该服务将降低活动性能。为了避免影响 应用程序性能,你应该在里面启动一个新的线程 服务。
但它没有说明在其他线程中运行的绑定服务。所以我决定以这种方式启动服务:
private void startRunService()
{
final Intent intent = new Intent(this, RunService.class);
Thread thread = new Thread(new Runnable() {
public void run() {
startService(intent);
}
});
thread.start();
}
private void startLinkerRunService()
{
final Intent intent = new Intent(this, LinkerRunService.class);
Thread thread = new Thread(new Runnable() {
public void run() {
startService(intent);
}
});
thread.start();
}
因此,在不同的服务中启动每项服务,我获得了意想不到的性能提升。但我不确定这是个好主意。
你知道它是否有副作用吗?
提前致谢。