0% found this document useful (0 votes)
2 views35 pages

Android Lab5

The document outlines the creation of three Android applications: one for listing Bluetooth devices and utilizing the Vibrator Service, another for displaying notifications using the Notification Service, and a third for scanning available Wi-Fi networks. Each application includes the necessary XML layout files and Java code, demonstrating the use of various Android APIs and permissions. The document provides detailed code snippets for the MainActivity and AndroidManifest.xml files for each application.

Uploaded by

sowmyarani912
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)
2 views35 pages

Android Lab5

The document outlines the creation of three Android applications: one for listing Bluetooth devices and utilizing the Vibrator Service, another for displaying notifications using the Notification Service, and a third for scanning available Wi-Fi networks. Each application includes the necessary XML layout files and Java code, demonstrating the use of various Android APIs and permissions. The document provides detailed code snippets for the MainActivity and AndroidManifest.xml files for each application.

Uploaded by

sowmyarani912
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

Android Programming

1. create an android application to get the Bluetooth devices and list of devices using Bluetooth
and Vibrator Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Switch
android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="BLUETOOTH"
android:textSize="25sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="getBTDevices"
android:text="GET BT DEVICES"
android:textSize="25sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="vibrate"
android:text="VIBRATE"
android:textSize="25sp" />

SVFGC CTA 1
Android Programming

<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

[Link]
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];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity

{ Switch s1;

SVFGC CTA 2
Android Programming

ListView lview;
BluetoothAdapter bAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

s1 = findViewById([Link].s1);
lview = findViewById([Link]);

bAdapter = [Link]();

if (bAdapter != null) {
[Link]([Link]());

[Link](
new [Link]() {
@Override
public void
onCheckedChanged( CompoundBut
ton buttonView, boolean
isChecked) {

if (isChecked) {
[Link]();
} else {
[Link]();
}
}
});
}
}

SVFGC CTA 3
Android Programming

public void getBTDevices([Link] v)

{ final ArrayList<String> list = new

ArrayList<>();

final ArrayAdapter<String> adapter =


new ArrayAdapter<>(
this,
[Link].simple_list_item_1,
list);

[Link](adapter);

IntentFilter filter =
new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(new BroadcastReceiver() {
@Override
public void
onReceive( Context
context, Intent
intent) {

BluetoothDevice device =
[Link](
BluetoothDevice.EXTRA_DEVICE);

if (device != null) {

String name = [Link]();


if (name == null) {
name = "Unknown Device";
}

SVFGC CTA 4
Android Programming

[Link](

SVFGC CTA 5
Android Programming

name + "\n" +
[Link]());

[Link]();
}
}
}, filter);

if (bAdapter != null) {
[Link]();
}
}

public void vibrate([Link] v) {

Vibrator vib =
(Vibrator) getSystemService(
Context.VIBRATOR_SERVICE);

if (vib != null) {

if ([Link].SDK_INT >=
[Link].VERSION_CODES.O) {

[Link](
[Link]( 1
000,
VibrationEffect.DEFAULT_AMPLITUDE));

} else {

[Link](1000);
}

SVFGC CTA 6
Android Programming

}
}
}

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]

<!-- Bluetooth Permissions -->


<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].BLUETOOTH_ADMIN"/>

<!-- Android 12+ -->


<uses-permission android:name="[Link].BLUETOOTH_CONNECT"/>
<uses-permission android:name="[Link].BLUETOOTH_SCAN"/>

<!-- Location Permission -->


<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"/>

<!-- Vibrator Permission -->


<uses-permission android:name="[Link]"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">

<activity
android:name=".MainActivity"
android:exported="true">

SVFGC CTA 7
Android Programming

<intent-filter>
<action android:name="[Link]"/>

<category
android:name="[Link]"/>
</intent-filter>

</activity>

</application>

</manifest>

Output:

SVFGC CTA 8
Android Programming

2. Create an android application to get the notifications on Notification Bar by Using


Notification Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/btnNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />

</LinearLayout>

[Link]
package [Link];

import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

SVFGC CTA 9
Android Programming

Button btnNotify;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

btnNotify = findViewById([Link]);

createNotificationChannel();

[Link](v -> showNotification());


}

private void showNotification() {

if ([Link].SDK_INT >=
[Link].VERSION_CODES.TIRAMISU) {

if (checkSelfPermission(
[Link].POST_NOTIFICATIONS)
!= [Link].PERMISSION_GRANTED) {

requestPermissions( ne
w String[]{
[Link].POST_NOTIFICATIONS
},
100);

return;
}
}

SVFGC CTA 10
Android Programming

[Link] builder =
new [Link](this, "my_channel")
.setSmallIcon([Link].ic_dialog_info)
.setContentTitle("Notification Service")
.setContentText(
"Welcome to Android Notification Service")
.setPriority(
NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat notificationManager =
[Link](this);

[Link](1, [Link]());
}

private void createNotificationChannel() {


if ([Link].SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel channel =
new NotificationChannel(
"my_channel",
"My
Notifications",
NotificationManager.IMPORTANCE_DEFAULT);

[Link]("Notification Channel");

NotificationManager manager =
getSystemService([Link]);

[Link](channel);
}
}
}

SVFGC CTA 11
Android Programming

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]

<uses-permission
android:name="[Link].POST_NOTIFICATIONS" />

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>

</activity>

</application>

</manifest>
Output:

SVFGC CTA 12
Android Programming

3. Create an android application to display available Wi-Fi devices and Paired Wi-Fi devices
by using Wi-Fi Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<Button
android:id="@+id/btnScan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan Wi-Fi
Networks"/>

<ListView
android:id="@+id/listWifi"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
[Link]
package [Link];

import static [Link].R.*;


import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

SVFGC CTA 13
Android Programming

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];
import [Link];

public class MainActivity extends AppCompatActivity {

private WifiManager wifiManager;


private Button btnScan;
private ListView listWifi;

private ArrayList<String> wifiNetworks;


private ArrayAdapter<String> adapter;

private static final int REQUEST_CODE = 100;

@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);

SVFGC CTA 14
Android Programming

setContentView([Link].activity_main);

SVFGC CTA 15
Android Programming

btnScan = findViewById([Link]);
listWifi = findViewById([Link]);

wifiManager = (WifiManager)
getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);

wifiNetworks = new ArrayList<>();

adapter = new ArrayAdapter<>(


this,
[Link].simple_list_item_1,
wifiNetworks);

[Link](adapter);

requestPermissionsIfNeeded();

[Link](v -> scanWifi());

registerReceiver( wifi
Receiver, new
IntentFilter(

WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

private void requestPermissionsIfNeeded() {

if ([Link].SDK_INT >=
Build.VERSION_CODES.TIRAMISU) {

SVFGC CTA 16
Android Programming

if
([Link]
n( this,
[Link].NEARBY_WIFI_DEVICES)
!= PackageManager.PERMISSION_GRANTED) {

[Link](
this,
new String[]{
[Link].NEARBY_WIFI_DEVICES
},
REQUEST_CODE);
}

} else {

if
([Link]
n( this,
[Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
||
[Link]( thi
s,
[Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

[Link](
this,
new String[]{

[Link].ACCESS_COARSE_LOCATION,
[Link].ACCESS_FINE_LOCATION
},

SVFGC CTA 17
Android Programming

REQUEST_CODE);
}

SVFGC CTA 18
Android Programming

}
}

@SuppressLint("MissingPermission")
private void scanWifi() {

try {

if ([Link].SDK_INT >=
Build.VERSION_CODES.TIRAMISU) {

if
([Link](
this,
[Link].NEARBY_WIFI_DEVICES)
!= PackageManager.PERMISSION_GRANTED) {

[Link](
this,
"Wi-Fi permission required",
Toast.LENGTH_SHORT).show();
return;
}

} else {

if
([Link](
this,
[Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
||
[Link]( this
,

SVFGC CTA 19
Android Programming

[Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

SVFGC CTA 20
Android Programming

[Link](
this,
"Location permission required",
Toast.LENGTH_SHORT).show();
return;
}
}

boolean success = [Link]();

if (!success) {

[Link](
this,
"Wi-Fi Scan Failed",
Toast.LENGTH_SHORT).show();
}

} catch (SecurityException e) {

[Link](
this,
"Permission denied",
Toast.LENGTH_SHORT).show();
}
}

private final BroadcastReceiver wifiReceiver


= new BroadcastReceiver() {

@SuppressLint("MissingPermission")
@Override

SVFGC CTA 21
Android Programming

public void onReceive(


Context context,
Intent intent) {

try {

List<ScanResult> results =
[Link]();

[Link]();

for (ScanResult result : results) {

[Link](
"SSID : " + [Link] + "\
nBSSID : " + [Link] +
"\nSignal : " + [Link] + " dBm");
}

[Link]();

} catch (SecurityException e) {

[Link](
context,
"Unable to read Wi-Fi networks",
Toast.LENGTH_SHORT).show();
}
}
};

@Override
public void onRequestPermissionsResult(

SVFGC CTA 22
Android Programming

int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {

[Link](
requestCode,
permissions,
grantResults);

if (requestCode == REQUEST_CODE) {

if ([Link] > 0
&& grantResults[0]
== PackageManager.PERMISSION_GRANTED) {

[Link](
this,
"Permission Granted",
Toast.LENGTH_SHORT).show();

} else {

[Link](
this,
"Permission Denied",
Toast.LENGTH_SHORT).show();
}
}
}

@Override
protected void onDestroy() {

SVFGC CTA 23
Android Programming

[Link]();

try {
unregisterReceiver(wifiReceiver);
} catch (Exception ignored) {
}
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]

<uses-permission
android:name="[Link].ACCESS_WIFI_STATE"/>
<uses-permission
android:name="[Link].CHANGE_WIFI_STATE"/>

<uses-permission
android:name="[Link].ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="[Link].ACCESS_FINE_LOCATION"/>

<uses-permission
android:name="[Link].NEARBY_WIFI_DEVICES"
android:usesPermissionFlags="neverForLocation"/>

<application
android:allowBackup="true"
android:label="WiFi Scanner"
android:theme="@style/[Link]">

<activity
android:name=".MainActivity"

SVFGC CTA 24
Android Programming

android:exported="true">

<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>

</activity>

</application>

</manifest>

Output:

SVFGC CTA 25
Android Programming

4. Create an android application to display X, Y Sensor values


by using Sensor Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:text="X Value: 0.0"
android:textSize="25sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Y Value: 0.0"
android:textSize="25sp" />

</RelativeLayout>

SVFGC CTA 26
Android Programming

[Link]
package [Link];

import static [Link].R.*;

import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity implements


SensorEventListener {

TextView tv1, tv2;


SensorManager sensorManager;
Sensor accelerometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

tv1 = findViewById([Link]);
tv2 = findViewById([Link].textView2);

// Get Sensor Service


sensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);

SVFGC CTA 27
Android Programming

if (sensorManager != null)
{ accelerometer =
[Link](Sensor.TYPE_ACCELEROMETER);
}
}

@Override
protected void onResume() {
[Link]();

if (accelerometer != null) {
[Link](
this,
accelerometer,
SensorManager.SENSOR_DELAY_NORMAL
);
}
}

@Override
protected void onPause() {
[Link]();
[Link](this);
}

@Override
public void onSensorChanged(SensorEvent event) {

if ([Link]() == Sensor.TYPE_ACCELEROMETER)
{

float x = [Link][0];

SVFGC CTA 28
Android Programming

float y = [Link][1];

[Link]("X Value is: " + x);


[Link]("Y Value is: " + y);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not required
}
}

Output:

SVFGC CTA 29
Android Programming

5. Create an android application to get the System Announcements


by using Broadcast Receiver.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for System Broadcast..."
android:textSize="24sp"
android:textColor="#000000"
android:layout_centerInParent="true"/>

</RelativeLayout>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">

<application
android:allowBackup="true"
android:label="Broadcast Receiver App"
android:theme="@style/[Link]">

<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>

SVFGC CTA 30
Android Programming

<category android:name="[Link]"/>
</intent-filter>
</activity>

</application>

</manifest>
[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

TextView tv;
MyReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

tv = findViewById([Link].tv1);

receiver = new MyReceiver(tv);

IntentFilter filter = new IntentFilter();


[Link](Intent.ACTION_POWER_CONNECTED);

SVFGC CTA 31
Android Programming

[Link](Intent.ACTION_POWER_DISCONNECTED);
[Link](Intent.ACTION_SCREEN_ON);
[Link](Intent.ACTION_SCREEN_OFF);
[Link](Intent.ACTION_AIRPLANE_MODE_CHANGED);
[Link](Intent.ACTION_HEADSET_PLUG);

registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
[Link]();
unregisterReceiver(receiver);
}
}
[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class MyReceiver extends BroadcastReceiver {

TextView tv;

public MyReceiver(TextView tv)


{ [Link] = tv;
}

@Override
public void onReceive(Context context, Intent intent) {

SVFGC CTA 32
Android Programming

String action = [Link]();

if (action == null || tv == null) return;

switch (action) {

case Intent.ACTION_POWER_CONNECTED:
[Link]("POWER CONNECTED");
break;

case Intent.ACTION_POWER_DISCONNECTED:
[Link]("POWER DISCONNECTED");
break;

case
Intent.ACTION_SCREEN_ON:
[Link]("SCREEN ON");
break;

case
Intent.ACTION_SCREEN_OFF:
[Link]("SCREEN OFF");
break;

case Intent.ACTION_AIRPLANE_MODE_CHANGED:
[Link]("AIRPLANE MODE CHANGED");
break;

case Intent.ACTION_HEADSET_PLUG:
[Link]("HEADSET PLUGGED");
break;
}
}

SVFGC CTA 33
Android Programming

SVFGC CTA 34
Android Programming

Output:

SVFGC CTA 35

You might also like