-1

I am trying to send a broadcast from a service but it is not received in the receiver. Following is my code

Bluetooth Service

public class BluetoothService extends Service {

public final static String TAG = "com.example.linvor.BluetoothService";
Intent intent1 = new Intent(TAG);
static boolean isRunning = false;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    android.os.Debug.waitForDebugger();

    super.onStartCommand(intent, flags, startId);
    //android.os.Debug.waitForDebugger();
    sendBroadcast(intent1);

    return START_NOT_STICKY;
}

}

My Broadcast receiver is as follows.

ServiceBroadCastReceiver

public class ServiceBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    MainActivity.myLabel.setText("BroadCAst received");
    if(intent.getAction().equals(BluetoothService.TAG)) {
    Log.i("Broadcast status","BroadCast received");
}
}
}

and my manifest declaration seems like this.

manifest

<receiver
        android:name=".ServiceBroadCastReceiver"
        android:label="@string/app_name"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.linvor.BluetoothService"></action>
        </intent-filter>
    </receiver>

One more thing:

when my service is running and sends broadcast to my activity(which is not received), my app shows app not responding.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    You have to register your Receiver on `LocalBroadcastManager`, not on a `Context`. Also, that `Service` is running on your app's main thread, so that `while` loop is likely causing the ANR. You should move that into a separate thread, and implement some sort of buffering, so it's not broadcasting every split second, which may end up causing an ANR from the Receiver's work. – Mike M. Aug 19 '17 at 13:50
  • Registering a receiver on ` LocalBroadcastManager ` also doesn't solves the problem. My receiver code is still not executing. – Shehryar Zaheer Aug 19 '17 at 14:57
  • Did you do something about the `while` loop on the main thread, and the loop timing? What happens if you temporarily remove the `while`, and just send a single broadcast or three from the `Service`? – Mike M. Aug 19 '17 at 15:04
  • Could you use `sendBroadcast(intent); `? – KeLiuyue Aug 19 '17 at 15:17
  • Sending broadcast without while loop is also not received. I assume that my broadcast receiver is not registering. Because if I call unregister receiver in onPause() and I quit my app, it crashes with the exception that the receiver was not registered but I did register in oncreate of the MainActivity. – Shehryar Zaheer Aug 19 '17 at 15:20
  • You also have to unregister on `LocalBroadcastManager`. You should [edit] your post with your current code. – Mike M. Aug 19 '17 at 15:28
  • I forgot to mention, `LocalBroadcastManager` does not work across processes, so if your `BluetoothService` is running in a separate process - i.e., it has a `process` attribute set on its `` element in the manifest - your setup isn't going to work with `LocalBroadcastManager`. – Mike M. Aug 19 '17 at 16:21
  • I have edited my question. Please check it out and let me know the solution. Thanks. – Shehryar Zaheer Aug 19 '17 at 18:34
  • You can check my answer.@ShehryarZaheer – KeLiuyue Aug 20 '17 at 01:04
  • @KeLiuyue I have tried your solution but that doesn't work. – Shehryar Zaheer Aug 20 '17 at 04:30

2 Answers2

1

Thanks to @KeLiuyue and @Mike M. for suggesting me solutions. But the problem was not in the broadcast code. The problem was in the service which was causing my app to not respond and hence I was getting problems with sending and receiving broadcasts in my app. And the reason for my service to not respond was this line.

android.os.Debug.waitForDebugger();

I just removed this line and everything is working fine.

0

Try this.

registerReceiver(receiver,new IntentFilter(BluetoothService.TAG));
Intent intent = new Intent();
//edited here ,your action
intent.setAction(BROADCAST_ACTION);
//send
sendBroadcast(intent);

Edited

// edited here , add your service
Intent startIntent = new Intent(this, BluetoothService.class);  
startService(startIntent);  

Note

  1. register in AndroidManifest.xml or in the java code

  2. then sendBroadcast in the java code

  3. unregister receiver in onDestroy() method

    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        unregisterReceiver(receiver);  
    } 
    
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • Could you edit your code?And put it here.@ShehryarZaheer – KeLiuyue Aug 20 '17 at 05:06
  • Ok I have created a new project where I have tried to send a broadcast through a service and I am able to receive that broadcast. I think that the problem in my current project is that ,in my service, I am trying to continuously get data from bluetooth which was causing my app to not respond and this might have been causing problem for the broadcast. But I don't know how to solve this issue. I mean issue of app not responding when my service is running. Could you please guide me what should I do. @KeLiuyue – Shehryar Zaheer Aug 20 '17 at 07:12