Search code examples
androidkotlinbroadcastreceiver

Broadcast Receiver does not get a message


I am creating two apps - first one sends a broadcast, second one should get it.

App that sends:

Permissions declared in manifest:

    <permission-group
            android:name="com.example.smbmp1.permissions"
            android:label="my permissions group"/>

    <permission
            android:name="com.example.smbmp1.permissions.PRODUCT_BROADCAST_PERMISSION"
            android:label="PRODUCT_BROADCAST_PERMISSION"
            android:permissionGroup="com.example.smbmp1.permissions"/>

    <uses-permission android:name="com.example.smbmp1.permissions.PRODUCT_BROADCAST_PERMISSION"/>

Code fragment responsible for sending:

    private fun populateProduct(product: Product) {
        Intent().also { intent ->
            intent.action = "com.example.smbmp1.PRODUCT_BROADCAST"
            intent.putExtra("id", product.id)
            intent.putExtra("name", product.name)
            intent.putExtra("price", product.price)
            intent.putExtra("quantity", product.quantity)
            sendBroadcast(intent, Manifest.permission.PRODUCT_BROADCAST_PERMISSION)
        }
    }

App that receives:

Registered receiver:

        <receiver
                android:name=".ProductReceiver"
                android:enabled="true"
                android:exported="true"
                android:process=":remote"
                android:permission="com.example.smbmp1.permissions.PRODUCT_BROADCAST_PERMISSION">
                <intent-filter >
                    <action android:name="com.example.smbmp1.PRODUCT_BROADCAST" />
                </intent-filter>
        </receiver>

Also declared permissions manifest (I was trying also without declaring in second app):

    <permission-group
            android:name="com.example.smbmp1.permissions"
            android:label="my permissions group"/>

    <permission
            android:name="com.example.smbmp1.permissions.PRODUCT_BROADCAST_PERMISSION"
            android:label="PRODUCT_BROADCAST_PERMISSION"
            android:permissionGroup="com.example.smbmp1.permissions"/>

    <uses-permission android:name="com.example.smbmp1.permissions.PRODUCT_BROADCAST_PERMISSION"/>

Simple receiver code:

package com.example.smbmp2

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class ProductReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Toast.makeText(context, "Receiver", Toast.LENGTH_LONG).show()
    }
}

Problem is, that receiver is not triggered. I went through all stack overflow questions and none of the answers helped. I were trying to set category to an intent, setting flags etc.

Any suggestions?

EDIT

I have also tried to register Broadcast Receiver in a dynamic way:

package com.example.smbmp2

import android.content.IntentFilter
import android.support.v7.app.AppCompatActivity
import android.os.Bundle



class MainActivity : AppCompatActivity() {

    private lateinit var productReceiver: ProductReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        productReceiver = ProductReceiver()
        registerReceiver(productReceiver, IntentFilter("com.example.smbmp1.PRODUCT_BROADCAST"), Manifest.permission.PRODUCT_BROADCAST_PERMISSION, null)
    }

    override fun onStop() {
        super.onStop()
        unregisterReceiver(productReceiver)
    }
}

Solution

  • I have solved the problem. First of all, I moved dynamic registration from onStart method to onCreate method and unregistration from onStop to onDestroy. After that I upgraded android studio and this solved the problem.