Broadcast receiver not working in Android Studio 4.0.0

I am new to Android Studio and the broadcast receiver is not working.

“Broadcast Received!!!” doesn’t appear on the screen.

Android Studio version: 4.0

Android Version in the emulator: Android 10 (Q)

SendBroadcast Project:

MainActivity.java:

package com.example.sendbroadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

public void sendBroadcast(View view) {
    Intent intent = new Intent();
    intent.setAction("com.example.sendbroadcast");
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(intent);
    System.out.println("Sent!!!");
}
  }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/send_broadcast_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendBroadcast"
    android:text="Send Broadcast"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ReceiveBroadcast project:

MyReceiver.java:

package com.example.recievebroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       System.out.println("Received!!!");
       Toast.makeText(context, "Broadcast Received!!!", Toast.LENGTH_LONG).show();
   }
 }

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="com.example.sendbroadcast"></action>
        </intent-filter>
    </receiver>
</application>

Answer

Implicit broadcasts like this one have been blocked on Android since Android 8.0, nearly three years ago.

Leave a Reply

Your email address will not be published. Required fields are marked *