我要求这样做是为了更深入地了解 Mono for Android,并在支持我的移动应用程序时做好更好的准备......
我注意到,当从“主启动器”活动中启动的工作线程调用 StartService 时,服务的 OnStartCommand 中的线程 ID 与活动线程 ID(1)相同,而不是启动它的工作线程 ID( 3)。
这是一个更抽象的表示:
myActivity : Activity
{
OnCreate
{
print_thread_id() // 1
start(thread_func)
}
thread_func()
{
print_thread_id() // 3 (not 2!, but anyway ...)
StartService(myService)
}
}
myService : Service
{
OnStartCommand
{
print_thread_id() // 1 also!!!
}
}
我还注意到在我的工作线程 thread_func 中,即使服务继续运行,StartService 也会返回。我认为对 StartService 的调用会阻塞,直到 myService 退出。
为什么 myService.OnStartCommand 中的线程 ID 与 thread_func 的不同,thread_func 的父级不是调用 StartService 的地方吗?
谢谢。