Not able to receive broadcast in other app

Viewed 23

I was learning broadcast receiver concept, so I tried making demo apps for sending broadcast and receiving custom private broadcast

This is my Broadcast Sender App code :

public class MainActivity extends AppCompatActivity {

TextView senderTextView;
Button sendButton;
private int counter;

//This is our anonymous class which will receive broadcast when sent from send button and set our text view
private final BroadcastReceiver innerReceiverAnonymousClass = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //if our (com.example.PRIVATE_BROADCAST) broadcast equals to the intent received here from intent filter
        if("com.example.PRIVATE_BROADCAST".equals(intent.getAction())){
            //counter variable for tracking no. of times broadcast is sent
            counter++;
            //then this means broadcast has been sent in mobile
            //set text
            senderTextView.setText("Broadcast Sent " + counter + " times");
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initializing views
    senderTextView = findViewById(R.id.senderTextView);
    sendButton = findViewById(R.id.sendButton);
    //Setting onClick Listener on sendButton
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Calling Broadcast Method
            Broadcast();
        }
    });


}
//Broadcast Method
private void Broadcast(){
    //Creating private broadcast event
    Intent intent = new Intent("com.example.PRIVATE_BROADCAST");
    //Adding String to our intent
    intent.putExtra("BROADCAST_EXTRA_DATA","This is Private Broadcast");
    //Sending our private broadcast to our android mobile
    sendBroadcast(intent);
}

@Override
protected void onStart() {
    super.onStart();
    //Intent filter will filter out intent from all the broadcasts happening in mobile
    // on basis of action and pass it to innerReceiverAnonymousClass
    IntentFilter intentFilter = new IntentFilter("com.example.PRIVATE_BROADCAST");
    registerReceiver(innerReceiverAnonymousClass,intentFilter);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(innerReceiverAnonymousClass);
}

This is my Broadcast Receiver app :

public class MainActivity extends AppCompatActivity {

TextView receiverTextView;

//Our anonymous private broadcast receiver class
//We can also make a separate class java class that will extend BroadcastReceiver class
//But we are using an anonymous so that we can get control of text view i.e. UI
private final BroadcastReceiver privateBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.example.PRIVATE_BROADCAST".equals(intent.getAction())){
            Log.d("APP_LOGS", "Broadcast Received");
            Toast.makeText(context, "Private Broadcast Received", Toast.LENGTH_SHORT).show();
            String broadcastString = intent.getStringExtra("BROADCAST_EXTRA_DATA");
            receiverTextView.setText("Broadcast Received : " + broadcastString);
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    receiverTextView = findViewById(R.id.receiverTextView);
}

@Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter("com.example.PRIVATE_BROADCAST");
    registerReceiver(privateBroadcastReceiver,intentFilter);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(privateBroadcastReceiver);
}

When I press send Broadcast button textview inside sender activity sets to "Broadcast sent 1 times which means our intent is getting broadcasted but toast from my receiver app doesn't show which means I am not able to get broadcast there. Can someone help me with this. Thanks in advance.

0 Answers
Related