Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver registered in manifest not getting called in Android Oreo

I have registered following receiver which is not getting called in Android Oreo but works on lower version devices.

<receiver
     android:name=".common.receiver.ConsultReceiver"
     android:exported="false">
       <intent-filter>
            <action android:name="APP_STARTED" />
            <action android:name="APP_STARTED_FROM_ORGANIC" />
       </intent-filter>
</receiver>  

Any help would be appreciated?

like image 584
Ragini Avatar asked Dec 01 '17 12:12

Ragini


People also ask

How do I know if my broadcast receiver is registered Android?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.

Which method is used to initiate broadcast receiver in Android?

The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

What does the receiver tag in Android manifest refer to?

Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running. There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element.


4 Answers

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.

like image 175
Nirmal Prajapat Avatar answered Nov 15 '22 20:11

Nirmal Prajapat


In general, you cannot use an implicit Intent (e.g., one with just an action string) for a broadcast on Android 8.0+.

Your <receiver> is not exported. This suggests one of three things:

  1. You are using this with a PendingIntent, such as for a Notification. If so, get rid of the <intent-filter> and use an explicit Intent (new Intent(this, ConsultReceiver.class)) as part of creating your PendingIntent that points to this receiver.

  2. You are using this as part of some IPC between multiple app processes in your app. In that case, also use an explicit Intent.

  3. You are using this receiver purely within one process within your app. In that case, get rid of the <receiver> and use something else (LocalBroadcastManager, an event bus, RxJava, LiveData, etc.).

like image 38
CommonsWare Avatar answered Nov 15 '22 20:11

CommonsWare


In Android 8.0 and above, use the following code to check when some app is installed or uninstalled.

class MainActivity : AppCompatActivity() {

    private val receiver = YourBroadcastReceiverClass()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val filter = IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED)
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
        filter.addDataScheme("package")

        registerReceiver(receiver, filter)
    }

    override fun onDestory() {
        unregisterReceiver(receiver)
        super.onDestroy()
    }
}
like image 28
Bugs Happen Avatar answered Nov 15 '22 18:11

Bugs Happen


Restrict the Intent to your package with setPackage.

like image 27
artkoenig Avatar answered Nov 15 '22 19:11

artkoenig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!