Радиоприемник не работает в Android Studio 4.0.0 - PullRequest
0 голосов
/ 21 июня 2020

Я новичок в Android Studio, и приемник вещания не работает.

"Передача получена !!!" не отображается на экране.

Android Версия Studio: 4.0

Android Версия в эмуляторе: 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:

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>

Ответы [ 2 ]

1 голос
/ 21 июня 2020

Неявные трансляции, подобные этому, были заблокированы на Android с Android 8.0 , почти три года go.

0 голосов
/ 21 июня 2020

Неявные трансляции заблокированы, чтобы нежелательные приложения не соответствовали вашему намерению и запускались (поэтому они будут работать только с приемниками, зарегистрированными во время выполнения).

Поскольку вы отправляете широковещание на Speci c Receiver, вы можете изменить свое намерение на явное , объявив целевой пакет:

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