Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding service by BroadcastReceiver

Tags:

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 
like image 603
yasso Avatar asked Sep 21 '13 10:09

yasso


People also ask

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What does a BroadcastReceiver do?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is the difference between service and BroadcastReceiver in Android?

A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.

Which intent is used by BroadcastReceiver?

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.


1 Answers

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

like image 146
user1923551 Avatar answered Oct 30 '22 18:10

user1923551