package org.pihole.dnsproxy; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.os.IBinder; import android.util.Log; public class WifiListenerService extends Service { public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener"; private BroadcastReceiver receiver; @Override public void onCreate () { super.onCreate(); this.receiver = new WifiListenerReceiver(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.start(); return START_STICKY; } @Override public void onDestroy() { this.stop(); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return null; } public void start() { // registerReceiver(this.receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)); registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel( WifiListenerService.NOTIFICATION_CHANNEL_ID, WifiListenerService.NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT ); manager.createNotificationChannel(channel); Notification notification = new Notification.Builder(this, WifiListenerService.NOTIFICATION_CHANNEL_ID) .setSmallIcon(R.drawable.logo) .setContentTitle("Pihole DNS Proxy - WiFi Listener") .setContentText("Listening for WiFi connection change") .build(); startForeground(2, notification); } public void stop() { unregisterReceiver(this.receiver); stopForeground(true); } }