通过BroadcastReceiver的绑定服务 [英] Binding service by BroadcastReceiver

查看:707
本文介绍了通过BroadcastReceiver的绑定服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用由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将不会允许它。

规则广播接收器:

  1. 广播接收机将不会有任何的UI(大部分)和将有只有背景的逻辑。
  2. 广播接收器将有10秒的最长时限,以其他方式完成其功能,就会死机。
  3. 您不应该做长时间运行的接收机操作或异步操作。     举例:一个。 preparing SD卡。      湾上传/从互联网上下载文件。              C。创建数据库文件。              ð。绑定到服务
  4. 请不要显示对话框,在广播接收器的用户。 (这是异步操作)
  5. 您可以使用面包或通知。
  6. 请不要写任何沉重的功能。

参考从开发的Andr​​oid 采取

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:

  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

这篇关于通过BroadcastReceiver的绑定服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆