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

Practical No. 20

The document provides code for two Android services: WiFiService and MyService. WiFiService enables Wi-Fi when started, while MyService displays toast messages indicating when the service starts and stops. Both services are controlled via buttons in their respective MainActivity classes, allowing users to start and stop the services easily.

Uploaded by

Riya
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)
11 views6 pages

Practical No. 20

The document provides code for two Android services: WiFiService and MyService. WiFiService enables Wi-Fi when started, while MyService displays toast messages indicating when the service starts and stops. Both services are controlled via buttons in their respective MainActivity classes, allowing users to start and stop the services easily.

Uploaded by

Riya
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

Practical no.

20
1. Write a program to start a Wi-Fi using service.
Program:
WiFi Service (WiFiService.java)
package com.example.wifiservice;

import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.util.Log;

public class WiFiService extends Service {


private static final String TAG = "WiFiService";

@Override
public IBinder onBind(Intent intent) {
return null; // No binding required
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
enableWiFi();
return START_STICKY;
}

private void enableWiFi() {


WifiManager wifiManager = (WifiManager)
getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager != null && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
Log.d(TAG, "Wi-Fi Enabled by Service");
}
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "WiFiService Stopped");
}
}
MainActivity.java
package com.example.wifiservice;

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

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(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, WiFiService.class));
}
});

stopServiceBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this, WiFiService.class));
}
});
}
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">

<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Wi-Fi Service" />

<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Wi-Fi Service"
android:layout_marginTop="10dp" />
</LinearLayout>

Output:
2. Write a program to display the following output.
Program:
MyService.java
package com.example.myserviceapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {


private static final String TAG = "MyService";

@Override
public IBinder onBind(Intent intent) {
return null; // No binding required
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Service Started");
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Service Stopped");
}
}

MainActivity.java
package com.example.myserviceapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, MyService.class));
}
});

stopServiceBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this, MyService.class));
}
});
}
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" />

<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service"
android:layout_marginTop="10dp" />
</LinearLayout>

Output:

You might also like