peter bence
peter bence

Reputation: 822

BroadcastReceiver doesn't receive the broadcast event message

I created my broadcast receiver AnprEventReciever that should be triggered when connectivity state changes, however it doesn't.

AnprEventReciever:

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

import bi.anpr.layouts.SplashActivity;

public class AnprEventReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("test","event recieved");
        Toast.makeText(context, "event recieved" , Toast.LENGTH_LONG).show();
    }
}

Manifest:

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

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

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

    <application
        android:name="bi.anpr.vlc.VLCApplication"
        android:allowBackup="false"
        android:icon="@mipmap/ic_anpr_launcher_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <receiver
            android:name="bi.anpr.core.AnprEventReciever"
            android:enabled="true"
            android:exported="true"
            android:label="StartMyServiceAtBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.CONNECTIVITY_CHANGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <activity
            android:name="bi.anpr.layouts.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" />
        <activity
            android:name="bi.anpr.layouts.VideoResourceActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name="bi.anpr.layouts.ZoneActivity"
            android:label="Zone Setter"
            android:screenOrientation="portrait" />

        <activity
            android:name="bi.anpr.layouts.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:screenOrientation="portrait" />

        <activity
            android:name="bi.anpr.layouts.SplashActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

    </application>

</manifest>

Please note that i'd tested almost every possible action that all failed except the BOOT_COMPLETED, where my BroadcastReceiver was successfully fired.

Upvotes: 2

Views: 1862

Answers (1)

peter bence
peter bence

Reputation: 822

Thanks for @TheWanderer who helped me through his comment to find the answer. In the Android 8.0 Behavior Changes Note developers mentioned the following:

Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).

So i tried to programatically registering my BroadcastReceiver like this below instead of creating a manifest entry for it:

private AnprEventReciever myReceiver;
private IntentFilter filter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_resource);

    filter = new IntentFilter();
    filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    myReceiver = new AnprEventReciever();
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(myReceiver, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(myReceiver);
}

It works perfectly, however unfortunately the action "android.intent.action.CONNECTIVITY_CHANGE" seems to be deprecated where i couldn't find it in the Intent class and i don't know if it is found elsewhere.

Note: as i go through reading i found that it is necessary to un-register the broadcast receiver when the activity pauses or destroys, otherwise you might get an error when trying to register or re-register it.


UPDATE:

Create intent filter with ConnectivityManager.CONNECTIVITY_ACTION instead of "android.intent.action.CONNECTIVITY_CHANGE" in order to receive connectivity changed broadcasts.

Upvotes: 1

Related Questions