广播接收器无法在 Android Studio 4.0.0 中工作

Broadcast receiver not working in Android Studio 4.0.0

我是 Android Studio 的新手,广播接收器不工作。

“收到广播!!!”没有出现在屏幕上。

Android工作室版本:4.0

Android模拟器中的版本:Android10(Q)

发送广播项目:

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>

接收广播项目:

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>

像这样的隐式广播自 Android 8.0 以来已在 Android 上被封锁,将近三年前。

隐式 广播被阻止以防止不需要的应用程序匹配您的意图并启动(这就是为什么它们只能与在运行时注册的接收器一起使用)。

由于您要向特定接收者发送广播,因此您可以通过声明目标包将您的意图修改为显式

public void sendBroadcast(View view) {
    Intent intent = new Intent();
    intent.setAction("com.example.sendbroadcast");
    // add this line to have intent delivered explicitly to your app
    // use package name of your ReceiveBroadcast project
    intent.setPackage("com.example.recievebroadcast");
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(intent);
    System.out.println("Sent!!!");
}