android - Creating custom BroadcastReceiver with custom action - Stack Overflow

admin2025-04-18  1

I have created my own BroadcastReceiver like so:

class BogusBroadcastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if(intent?.action == ACTION_NAME){
            Log.d { "Received Broadcast!!" }
        }
    }

    private companion object {
        const val ACTION_NAME = "com.myapp.intent.BOGUS_ACTION"
    }
}

And i declare it in my manifest like this:

 <application
        android:name=".MyApp"
        android:banner="@mipmap/ic_banner"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">

        <receiver android:name=".test.BogusBroadcastReceiver" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="com.myapp.intent.BOGUS_ACTION" />
            </intent-filter>
        </receiver>

...
</application>

However when i try to trigger it from adb like:

adb shell am broadcast -a com.myapp.intent.BOGUS_ACTION

I get:

Broadcasting: Intent { act=com.myapp.intent.BOGUS_ACTION flg=0x400000 }
Broadcast completed: result=0

But the log line never gets printed and my BroadcastReceiver doesnt run, regardless of the app being in foreground or background.

What am i missing here?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744969433a277397.html

最新回复(0)