I try to develop an application that in the future can run a service, the service I want to run when I connect to a network.
At the moment I only need the receiver to print in the log (using react-native log-android) that has been connected or disconnected, or to visualise a toast with a message, all this in the background not in the foreground.
Try the following,
First I put the receiver in the AndroidManifest.xml
<application>
...
<receiver android:name="com.air_fighers_react_native.receiver.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
Even add the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Create the receiver folder in java > com > air_fighters_react_native > receiver and inside the NetworkChangeReceiver.java file with the following code:
package com.air_fighters_react_native.receiver;
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class NetworkChangeReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Cambio de conexion.", Toast.LENGTH_LONG).show();
}
}
With this it is supposed that it should be enough, however when running the application there are no problems, but when I close the application and activate and deactivate the WiFi the toast message is not displayed, not even when I have the application in the foreground.
I have already tried changing receiver to:
<receiver android:name=".NetworkChangeReceiver">
And changing NetworChangeReceiver to the same level as MainApplication and MainActivity in folders.