当前位置:网站首页>Interview questions about Android Service

Interview questions about Android Service

2022-08-11 08:11:00 is a small A

It has been two months since the last interview. I remembered the interview topic and wanted to share it with you. I hope you can use it.

1.Can the onCreate callback function of Service do time-consuming operations?

No

Service's onCreate is called in the main thread (ActivityThread), and time-consuming operations will block the UI

If you need to do time-consuming operations - thread and Handler mode

2. Do you know IntentService and in what scenarios do you use IntentService?

Compared with the parent class Service, the biggest feature of IntentService is that it can directly perform time-consuming operations in its callback function onHandleIntent, without having to open a thread

Rationale:

The member variable Handler of IntentService already belongs to the worker thread when it is initialized, and then handleMessage, including onHandleIntent and other functions run in the worker thread

If the understanding of IntentService is limited to this, there will be a point of view that IntentService is very tasteless, because it is not troublesome to open threads in Service for time-consuming operations

IntentService Features:

It is to call the onHandleIntent function multiple times (that is, there are multiple time-consuming tasks to be executed), and multiple time-consuming tasks will be executed in sequence


The principle is that its built-in Handler is associated with the task queue, and the Handler takes the task execution through the looper and executes it sequentially


This feature can solve the problem that multiple time-consuming tasks need to be executed sequentially.And if you only use service and open multiple threads to perform time-consuming operations, it will be difficult to manage

原网站

版权声明
本文为[is a small A]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110644322300.html