elmazzun
elmazzun

Reputation: 1086

How to notify when wifi is switched on (/off) by a foreground app?

I'm trying to run an app in background which tells me when the status of wifi is modified;

public class BackgroundJobs extends Service {

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("BACKJOBS", "here we are");
    wifiInfo wifiHandler = new wifiInfo(this);
    // wifistatus checks if wifi is on or off, returning a boolean
    if(wifiHandler.wifiStatus())
        Log.i("BACKJOBS", "WIFI ON");
    else
        Log.i("BACKJOBS", "WIFI OFF");
    return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    //TODO for communication return IBinder implementation
    return null;
}
}

In my MainActivity I start the service as follows:

startService(new Intent(this, BackgroundJobs.class));

and in AndroidManifest.xml:

<service android:name=".gestioneServizi.BackgroundJobs"/>

I start my app, I go back to my home pushing the home button, i switch on or off the wifi, but I don't see the logs "BACKJOBS".

Am i misunderstanding something or I'm working with the wrong method?

Upvotes: 0

Views: 383

Answers (1)

Emanuel
Emanuel

Reputation: 8106

For this case you should a BroadcastReceiver.

Manifest:

   <receiver android:name="com.app.receiver.ConnectionReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
        </intent-filter>
   </receiver> 

Receiver:

 public class ConnectionReceiver extends BroadcastReceiver {
   private static String lastActiveNetworkName = null;
   private ConnectivityManager connectivityManager;
   private NetworkInfo wifiInfo;

   @Override
   public void onReceive(final Context context, Intent intent) {
    try {
      connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      NetworkInfo network = connectivityManager.getActiveNetworkInfo();

     if (network != null) {
            String networkName = network.getTypeName();
            if (!networkName.equals(lastActiveNetworkName)) {
                lastActiveNetworkName = networkName;
            } else {
                Log.d(TAG, "Network equals lastnetwork:" + lastActiveNetworkName);
            }
        }

        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

            if (networkInfo.isConnected()) {
                Log.d(TAG, "Wifi is connected: " + String.valueOf(networkInfo));
            }
        } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected()) {
                Log.d(TAG, "Wifi is disconnected: " + String.valueOf(networkInfo));
            }
        }
}

Upvotes: 2

Related Questions