简体   繁体   English

广播接收器在 Android Studio 4.0.0 中不起作用

[英]Broadcast receiver not working in Android Studio 4.0.0

I am new to Android Studio and the broadcast receiver is not working.我是 Android Studio 的新手,广播接收器无法正常工作。

"Broadcast Received.!!" “广播收到了。!!” doesn't appear on the screen.没有出现在屏幕上。

Android Studio version: 4.0 Android Studio 版本:4.0

Android Version in the emulator: Android 10 (Q) Android 模拟器中版本:Android 10 (Q)

SendBroadcast Project:发送广播项目:

MainActivity.java: 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 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: 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: 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>

Implicit broadcasts like this one have been blocked on Android since Android 8.0 , nearly three years ago.近三年前,自Android 8.0以来,Android 上已阻止此类隐式广播。

Implicit broadcasts are blocked to prevent unwanted apps from matching your intent and starting (that's why they will only work with receivers registered at runtime).隐式广播被阻止以防止不需要的应用程序匹配您的意图并启动(这就是为什么它们只能与在运行时注册的接收器一起使用)。

Since you're sending broadcast to specific receiver you can modify your intent to be explicit by declaring target package:由于您正在向特定接收器发送广播,因此您可以通过声明目标 package 来将您的意图修改为显式

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!!!");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM