0

I'm trying to send a simply customized broadcast by clicking a button, and the receiver will push a toast as it receives the broadcast. But as I clicked the button, nothing toasted with an error message in the logcat:

2021-01-18 16:23:18.870 480-480/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe

The MainActivity is:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest2.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }

And the BroadcastReceiver is:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Toast.makeText(context, "My Broadcast Received", Toast.LENGTH_SHORT).show();
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

I have also registered the action inside the BroadcastReceiver:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest2">


    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BroadcastTest2"
        >
        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest2.MY_BROADCAST" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Could anyone help me with that? Many thanks!

1 Answers1

0

for in app broadcast communication you have to register the broadcast receiver in the Activity, in your case you missing an syntax inside onCreate like this one:

BroadcastReceiver myReceiver = new MyBroadcastReceiver();
registerReceiver(myReceiver, new IntentFilter('com.example.broadcasttest2.MY_BROADCAST'));

for more reference - Broadcast Receiver class and registerReceiver method

Karthick
  • 281
  • 2
  • 7