PRACTICAL NO-20
MainActivity==
package com.example.wifihotspot;
import   android.content.Intent;
import   android.os.Bundle;
import   android.view.View;
import   androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startHotspot(View view) {
        startService(new Intent(this, WiFiHotspotService.class));
    }
    public void stopHotspot(View view) {
        stopService(new Intent(this, WiFiHotspotService.class));
    }
}
.java=
package com.example.wifihotspot;
import   android.app.Notification;
import   android.app.NotificationChannel;
import   android.app.NotificationManager;
import   android.app.Service;
import   android.content.Context;
import   android.content.Intent;
import   android.net.wifi.WifiManager;
import   android.os.Build;
import   android.os.IBinder;
import   android.util.Log;
import   android.widget.Toast;
import   androidx.annotation.Nullable;
import   androidx.core.app.NotificationCompat;
import   java.lang.reflect.Method;
public class WiFiHotspotService extends Service {
    private static final String TAG = "WiFiHotspotService";
    private static final String CHANNEL_ID = "WiFiHotspotServiceChannel";
    private WifiManager wifiManager;
    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
        startForeground(1, getNotification());
        wifiManager = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            toggleHotspot(true);
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        toggleHotspot(false);
    }
    private void toggleHotspot(boolean enable) {
        try {
            Method method = wifiManager.getClass().getMethod("setWifiApEnabled",
android.net.wifi.WifiConfiguration.class, boolean.class);
            method.invoke(wifiManager, null, enable);
            String message = enable ? "WiFi Hotspot Started" : "WiFi Hotspot
Stopped";
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
            Log.d(TAG, message);
        } catch (Exception e) {
            Log.e(TAG, "Error toggling WiFi hotspot", e);
        }
    }
    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "WiFi Hotspot Service",
                    NotificationManager.IMPORTANCE_LOW
            );
            NotificationManager manager =
getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(serviceChannel);
            }
        }
    }
    private Notification getNotification() {
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("WiFi Hotspot Service")
                .setContentText("Running...")
                .setSmallIcon(R.drawable.ic_wifi)
                .build();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}