arenutty
arenutty

Reputation: 11

Android BroadcastReceiver returns zero

I make a broadcast call on my Android 10 device, but it returns zero. I tried the suggested solutions but they didn't work. How can I solve it?

manifest

<application
        <receiver android:name=".ExampleReceiver" android:exported="true">
            <intent-filter>
                <action android:name="com.example.myapplication.Test"/>
            </intent-filter>
        </receiver>
</application>

ExampleReceiver.java

public class ExampleReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String test = "Hello World";
        setResultData(test);
    }

}
am broadcast -a com.example.myapplication.Test
Broadcasting: Intent { act=com.example.myapplication.Test flg=0x400000 }
Broadcast completed: result=0

Upvotes: 1

Views: 847

Answers (1)

Bashtanov Prokhor
Bashtanov Prokhor

Reputation: 196

Documentation says

If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.

So you need to register your receiver in code.

registerReceiver(new ExampleReceiver(), new IntentFilter("com.example.myapplication.Test"));

successful result picture

Upvotes: 2

Related Questions