0% found this document useful (0 votes)
7 views6 pages

PRG 20 Mad

The document outlines the implementation of a Wi-Fi service application for Android, including the layout (activity_main.xml), main activity (MainActivity.java), Android manifest (AndroidManifest.xml), and the service class (WifiService.java). The app allows users to start and stop the Wi-Fi service through buttons in the user interface. It requires permissions to change and access the Wi-Fi state and displays a toast message when the Wi-Fi is turned on.

Uploaded by

Ashwin Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

PRG 20 Mad

The document outlines the implementation of a Wi-Fi service application for Android, including the layout (activity_main.xml), main activity (MainActivity.java), Android manifest (AndroidManifest.xml), and the service class (WifiService.java). The app allows users to start and stop the Wi-Fi service through buttons in the user interface. It requires permissions to change and access the Wi-Fi state and displays a toast message when the Wi-Fi is turned on.

Uploaded by

Ashwin Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Write a program to start a Wi-Fi using service

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:ignore="ButtonStyle">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wifi Service App"
android:layout_gravity="center"
android:textSize="40dp"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:layout_marginTop="50dp"
android:layout_gravity="center"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Stop Service"
tools:ignore="HardcodedText" />
</LinearLayout>
MainActivity.java

package com.example.wifiserviceapp;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

Button startServiceBtn = findViewById(R.id.startService);


Button stopServiceBtn = findViewById(R.id.stopService);

startServiceBtn.setOnClickListener(v -> startService(new Intent(this,


WifiService.class)));
stopServiceBtn.setOnClickListener(v -> stopService(new Intent(this,
WifiService.class)));
}
}
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Wifiserviceapp"
tools:targetApi="31">
<service android:name=".WifiService" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>

</manifest>
WifiService.java
package com.example.wifiserviceapp;

import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.widget.Toast;

class WifiService extends Service {


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
WifiManager wifiManager = (WifiManager)
getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager != null && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
Toast.makeText(this, "Wi-Fi Turned ON", Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}

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

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:ignore="ButtonStyle">

<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:layout_marginTop="50dp"
android:layout_gravity="center"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Stop Service"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service Started"
android:layout_gravity="center"
android:textSize="40dp"
android:layout_marginTop="50dp"
android:background="@color/black"
/>
</LinearLayout>

You might also like