Broadcastreceiver is not working

Asked

Viewed 61 times

0

My Broadcastreceiver isn’t working, I don’t know what’s wrong, in my Manifest there’s this

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<receiver
        android:name=".BroadcastReceiver.NetworkStateReceiver"
        android:enabled="true"
        android:permission="android.permission.ACCESS_NETWORK_STATE"
        tools:ignore="InnerclassSeparator">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

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

and I’m trying to run a code every time the mobile phone exchanges Wi-Fi connection, for example, is connected on NETWORK A, but for some reason disconnects from NETWORK A and connects on NETWORK B, this action I want to intercept and call the Broadcastreceiver, i am developing an app that keeps a record of all Wi-Fi networks on which the mobile phone has connected,

public class NetworkStateReceiver extends BroadcastReceiver {

private ConnectionFirebase connectionFirebase;

@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    onNetworkChange(networkInfo, context);
}

private void onNetworkChange(NetworkInfo networkInfo, Context context){
    if (connectionFirebase == null){
        connectionFirebase = new ConnectionFirebase();
    }
    if (networkInfo != null){
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED
                || networkInfo.getState() == NetworkInfo.State.CONNECTING){
            connectionFirebase.saveNameWifi(context.getApplicationContext());
        }
    }
}

}

this and my Broadcastreceiver, I’m still in college and trying to learn how to use Broadcastreceiver, my problem is that the app doesn’t see when the phone changes the connection to Wi-Fi.

  • Make sure this is right: android:name=".BroadcastReceiver.NetworkStateReceiver" and remove <category android:name="android.intent.category.DEFAULT" />

  • 1

    .Broadcastreceiver is the name of my Package and . Networkstatereceiver and the name of my class that inherits from Broadcastreceiver, this right.

  • 1

    i removed <Category android:name="android.intent.Category.DEFAULT" /> and it still doesn’t work

  • I need this to work even when the app is not open

  • The solution to the other topic worked, but it only works if the app is open, I need it to be working before the user opens the app.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.