0

我是 Android Studio 新手,广播接收器无法正常工作。

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

安卓工作室版本:4.0

模拟器中的Android版本:Android 10 (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>
4

2 回答 2

1

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

于 2020-06-21T16:00:56.823 回答
0

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

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

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!!!");
}
于 2020-06-21T16:40:04.043 回答