Search code examples
androidbroadcastreceiveroncreate

Activity starting Broadcast Receiver to check network status


I have an activity on which I am initialising a broadcast receiver. I want this broadcast receiver to to call a function in my activity once it has received the message that the network status of the phone has changed.

Unfortunately it seems like the broadcast receiver never receives this message since the onReceive function is never called.

Here the Receiver:

private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            final android.net.NetworkInfo mobile = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (!wifi.isAvailable() || !mobile.isAvailable()) {
                Log.d("debug"," in the if");
                Handler handler = new Handler();
                handler.postDelayed(sendUpdatesToUI, 10);
                Log.d("Network Available ", "Flag No 1");
            }
        }
    };

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            update();
        }
    };

    private void update() {
        Log.d("debug", "got update");
    }

I call register it with this line in the onCreate function:

 registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));

and here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rtuya.secmere">

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar" />

    </application>

</manifest>

EDIT

I have added this in my manifest but still no result:

<receiver
            android:name="NetworkReceiver"
            android:label="NetworkReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

Solution

  • Broadcast receiver also needs to be declared in your manifest.xml

    Add

    <receiver android:name=".ReceiverName" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
    </receiver>
    

    You might want to place receiver in it's own class instead of doing it anonymously.