Meet Maheshwari
Meet Maheshwari

Reputation: 140

Static BroadcastReceiver not working ANDROID

I'm trying to create a static broadcast receiver, but it is not working. I'm using API level 25.

AndroidManifest

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

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            tools:ignore="GoogleAppIndexingWarning">

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

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

        <receiver
                android:name=".ScreenReceiver"
                android:enabled="true"
                android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.SCREEN_OFF"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

ScreenReceiver.java

package com.example.broadcastreceivertry;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("Screen Update", "Screen ON/OFF");
    }

}

MainActivity.kt

package com.example.broadcastreceivertry

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import android.widget.Button

class MainActivity : AppCompatActivity() {

    var SCREEN_INTENT = "Screen.Intent"

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

After the code is running, it is not able to receive the broadcasts made by android system.

If I dynamically register these receivers in my MainActivity then there is no issue. But in this static registered receiver, I'm not able to get broadcast.

Upvotes: 3

Views: 6006

Answers (1)

Jasurbek
Jasurbek

Reputation: 2966

Note: 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.

That means if you really need broadcast receiver then you should dynamically register and unregister it by code

As @MD mentioned this URL illustrates why it is not allowed and how to deal with this issue.

Android 7.0

Android 7.0 (API level 24) and higher don't send the following system broadcasts:

ACTION_NEW_PICTURE
ACTION_NEW_VIDEO

Also, apps targeting Android 7.0 and higher must register the CONNECTIVITY_ACTION broadcast using

registerReceiver(BroadcastReceiver, IntentFilter)

. Declaring a receiver in the manifest doesn't work.

Official site will describe all changes in broadcast reciever

Upvotes: 3

Related Questions