PRACTICAL NO-20
MainActivity==
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void startHotspot(View view) {
startService(new Intent(this, [Link]));
}
public void stopHotspot(View view) {
stopService(new Intent(this, [Link]));
}
}
.java=
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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() {
[Link]();
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() {
[Link]();
toggleHotspot(false);
}
private void toggleHotspot(boolean enable) {
try {
Method method = [Link]().getMethod("setWifiApEnabled",
[Link], [Link]);
[Link](wifiManager, null, enable);
String message = enable ? "WiFi Hotspot Started" : "WiFi Hotspot
Stopped";
[Link](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 ([Link].SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"WiFi Hotspot Service",
NotificationManager.IMPORTANCE_LOW
);
NotificationManager manager =
getSystemService([Link]);
if (manager != null) {
[Link](serviceChannel);
}
}
}
private Notification getNotification() {
return new [Link](this, CHANNEL_ID)
.setContentTitle("WiFi Hotspot Service")
.setContentText("Running...")
.setSmallIcon([Link].ic_wifi)
.build();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}