0% found this document useful (0 votes)
283 views4 pages

Mad 20

The document contains code for an Android application. It defines a MainActivity class that extends AppCompatActivity. The activity launches a ToastService when a button is clicked. The ToastService runs in the background and displays Toast messages when it starts and stops to confirm its status. The activity and service code are accompanied by XML layout files defining the user interface with buttons to control the service.

Uploaded by

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

Mad 20

The document contains code for an Android application. It defines a MainActivity class that extends AppCompatActivity. The activity launches a ToastService when a button is clicked. The ToastService runs in the background and displays Toast messages when it starts and stops to confirm its status. The activity and service code are accompanied by XML layout files defining the user interface with buttons to control the service.

Uploaded by

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

Name: Ekambe Ganesh Dattatraya

Roll No. :53


Practical No. : 20
package com.example.ac_mad;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import
androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest; import


android.content.Context;
import android.content.pm.PackageManager; import
android.net.wifi.WifiManager; import android.os.Bundle;
import android.view.View; import
android.widget.Button; import
android.widget.Toast;

public class MainActivity extends AppCompatActivity { private int


CODE = 1109;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnWifi = (Button) findViewById(R.id.wifiBtn);
btnWifi.setOnClickListener((View v)->{
int permissionStatus =
ContextCompat.checkSelfPermission(this,
android.Manifest.permission.CHANGE_WIFI_STATE);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]

{Manifest.permission.CHANGE_WIFI_STATE},}
29 WifiManager wifi =
(WifiManager)
getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);});
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == CODE && grantResults.length > 0 && grantResults[0]


== PackageManager.PERMISSION_GRANTED) {

} else {
Toast.makeText(this, "NO PERM", Toast.LENGTH_SHORT).show();
}
}
}

.xml file

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<Space android:layout_width="match_parent"
android:layout_height="159dp" />

<Button
android:id="@+id/wifiBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start WiFi" />
</LinearLayout>
.java file :

package com.example.ac_mad;

import androidx.appcompat.app.AppCompatActivity; import

android.content.Intent;
import android.os.Bundle;
import android.view.View; import
android.widget.Button;

public class MainActivity extends AppCompatActivity { protected void


onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
import androidx.annotation.Nullable;

public class ToastService extends Service {


public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this,
"Started Service with id "+ startId,
Toast.LENGTH_SHORT).show();
return START_STICKY; 14 }

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed ",
Toast.LENGTH_LONG).show();
}
@Nullable @Override
public IBinder onBind(Intent intent) { return null;
}

.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<Space android:layout_width="match_parent"
android:layout_height="159dp" />

<Button
android:id="@+id/start_btn"
android:layout_width="match_pare nt"
android:layout_height="wrap_cont
ent"android:text="Start Service"
/>

<Space
android:layout_width="match_par ent"
android:layout_height="36dp" />

<Button
android:id="@+id/stop_btn"
android:layout_width="match_pare nt"
android:layout_height="wrap_cont
ent"android:text="Stop Service"
/>
</LinearLayout>

You might also like