Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcastreceiver components are not allowed to bind to services in android

Here is what I am doing:

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        ConnectivityManager conn = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = conn.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (isConnected == true) {

        // initChatHub();
         Intent serviceIntent = new Intent(context, ChatService.class);
        Toast.makeText(context, "Connected", 1000).show();
        serviceIntent.putExtras(intent);
        context.startService(serviceIntent);
        context.bindService(serviceIntent, mServiceConnection,
                Context.BIND_AUTO_CREATE);

    } else {
        Toast.makeText(context, "Disconnected", 1000).show();
    }
}

when network state change then application becomes crash when i data-connection off then there is no Error coming when data connection On then app becomes crash and this Exception coming please suggest me how to fix it.

enter image description here

like image 821
Edge Avatar asked Mar 18 '15 04:03

Edge


People also ask

Which are the BroadcastReceiver are available in Android?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.

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.

How pass data from BroadcastReceiver to activity in Android?

getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme. Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.

What file is used to register the BroadcastReceiver?

You register this broadcast receiver either in the manifest file or in the code. These events are intents. So, whenever these kinds of events happen, an intent is triggered.


1 Answers

See here Context.bindService :

Note: this method can not be called from a BroadcastReceiver component....

So, use BroadcastReceiver.peekService which return IBinder from running Service:

 Intent serviceIntent = new Intent(context, ChatService.class);
 context.startService(serviceIntent);
 if (isConnected == true) {
  IBinder binder = peekService(context, new Intent(context, ChatService.class));
  Toast.makeText(context, "Connected", 1000).show();
  if (binder != null){
    mChatService = ((LocalBinder) binder).getService();
        //.... other code here          
  }
} 
like image 160
ρяσѕρєя K Avatar answered Sep 23 '22 18:09

ρяσѕρєя K