0% found this document useful (0 votes)
12 views2 pages

Practialno 20

The document contains code for an Android application that manages a WiFi hotspot service. It includes a MainActivity that starts and stops the hotspot service, and a WiFiHotspotService class that handles the actual hotspot functionality, including creating a notification channel and toggling the hotspot state. The service uses reflection to access the method for enabling or disabling the WiFi hotspot and provides user feedback through Toast messages and logs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Practialno 20

The document contains code for an Android application that manages a WiFi hotspot service. It includes a MainActivity that starts and stops the hotspot service, and a WiFiHotspotService class that handles the actual hotspot functionality, including creating a notification channel and toggling the hotspot state. The service uses reflection to access the method for enabling or disabling the WiFi hotspot and provides user feedback through Toast messages and logs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;
}
}

You might also like