问题描述
我的应用程序使用由BOOT_COMPLETE的BroadcastReceiver启动的服务,在运行我得到一个错误
我的code:
公共类谟扩展的BroadcastReceiver {
公共无效的onReceive(上下文的背景下,意图意图){
意图=新的意图(背景下,ScreenshotService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.bindService(意向,aslServiceConn,Context.BIND_AUTO_CREATE);
}
}
错误:
了java.lang.RuntimeException:无法启动接收器com.example.projet:android.content.ReceiverCallNotAllowedException:BroadcastReceiver的组件不能够结合服务
每个人都应该不绑定从广播接收机的服务。其理由是广播接收器是轻重量的组件,其中,它具有完成其功能与在最高不超过10秒。否则机器人可以有力地杀死你的接收器。绑定(建立连接)服务可能需要超过10秒,在一些最坏的情况下,这就是为什么Android将不会允许它。
规则广播接收器:
- 广播接收机将不会有任何的UI(大部分)和将有只有背景的逻辑。
- 广播接收器将有10秒的最长时限,以其他方式完成其功能,就会死机。
- 您不应该做长时间运行的接收机操作或异步操作。 举例:一个。 preparing SD卡。 湾上传/从互联网上下载文件。 C。创建数据库文件。 ð。绑定到服务
- 请不要显示对话框,在广播接收器的用户。 (这是异步操作)
- 您可以使用面包或通知。
- 请不要写任何沉重的功能。
参考从开发的Android 采取
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 BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
intent = new Intent(context, ScreenshotService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.bindService(intent, aslServiceConn, Context.BIND_AUTO_CREATE);
}
}
error:
java.lang.RuntimeException: Unable to start receiver com.example.projet: android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to bind to services
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:
- Broadcast receivers will not have any UI(mostly) and it will have only background logic.
- Broadcast receivers will have the maximum time limit of 10 sec to finish its functionality otherwise it will crash.
- 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
- Do not show dialog to the user in broadcast receiver. (this is asynchronous operation)
- You can use " toast" or "Notifications".
- Don’t write any heavy functionalities.
Ref taken from developer android
这篇关于通过BroadcastReceiver的绑定服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!