Maven Maverick Maven Maverick - 5 months ago 20
Java Question

Android - Broadcast Receiver not logging messages

I have a

service
which contains a
Broadcast Receiver
that looks for Wi-Fi state change and makes a toast when enabled or disabled. But, my code does not toast the wi-fi state change messages though it toasts the messages related to service create, start and destroy.

WLANService.java



public class WLANService extends Service {
public WLANService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created!", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

int state = 0;
String message = intent.getStringExtra("message");
Toast.makeText(this, "Service Started & " + message, Toast.LENGTH_LONG).show();

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled()) {
Log.i("Wi-Fi-State", "Wi-Fi is On!");
}else {
Log.i("Wi-Fi-State", "Wi-Fi is Off!");
}
}
};
return START_STICKY;
}

@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed!", Toast.LENGTH_LONG).show();
}
}


MainActivity.java



public class MainActivity extends AppCompatActivity {

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

public void startTheService(View v) {
Intent intent = new Intent(this, WLANService.class);
intent.putExtra("message", "I got your message!");
startService(intent);
}

public void stopTheService(View v) {
stopService(new Intent(this, WLANService.class));
}
}


AndroidManifest.xml


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

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".WLANService"
android:enabled="true"
android:exported="false">
</service>
</application>

</manifest>

Answer

You could do something like this:

public class WLANService extends Service {
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            boolean connected = info.isConnected();
            if(connected) {
                Toast.makeText(context, "WIFI CONNECTED!", Toast.LENGTH_LONG).show();
                Log.i("Wi-Fi-State", "Wi-Fi is On!");
            } else {
                Toast.makeText(context, "WIFI DISCONNECTED!", Toast.LENGTH_LONG).show();
                Log.i("Wi-Fi-State", "Wi-Fi is Off!");
            }
        }
    }

    public WLANService() {
        // why do you need this, by the way?
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Created!", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // registering your receiver
        registerReceiver(receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed!", Toast.LENGTH_LONG).show();
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

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

        // starting your Service
        startTheService();
    }

    public void startTheService() {
        Intent intent = new Intent(this, WLANService.class);
        intent.putExtra("message", "I got your message!");
        startService(intent);
    }
}