Binding service by BroadcastReceiver

后端 未结 8 1046
春和景丽
春和景丽 2021-02-12 15:32

My Application uses a service that is started by a BOOT_COMPLETE BroadcastReceiver, in run i\'m getting an error

my code:

public class projet extends Br         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-02-12 16:11

    One should not bind a service from Broadcast receiver. The reason is broadcast receivers are light weight components, where it has to finish its functionality with in not more than 10 seconds maximum. Else android may forcefully kill your receiver. Binding (establishing connection to) a service may take more than 10 seconds in some worst cases, that's why android won't allow it.

    Rules for Broadcast Receivers:

    1. Broadcast receivers will not have any UI(mostly) and it will have only background logic.
    2. Broadcast receivers will have the maximum time limit of 10 sec to finish its functionality otherwise it will crash.
    3. You should not do long running operations or asynchronous operations in the receiver. Example: a. Preparing SD card. b. Uploading / Downloading files from internet. c. Creating Db files. d. Binding to services
    4. Do not show dialog to the user in broadcast receiver. (this is asynchronous operation)
    5. You can use “ toast” or “Notifications”.
    6. Don’t write any heavy functionalities.

    Ref taken from developer android

提交回复
热议问题