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

Android Lab3

The document provides instructions for creating multiple Android applications, including one that displays WhatsApp videos in a grid view using a custom adapter, another that uses a WebView to display a webpage, and a third that utilizes fragments to show different web pages. Additionally, it details an application that stores user data using Shared Preferences. Each section includes code snippets for layout files and Java classes necessary for implementation.

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 views92 pages

Android Lab3

The document provides instructions for creating multiple Android applications, including one that displays WhatsApp videos in a grid view using a custom adapter, another that uses a WebView to display a webpage, and a third that utilizes fragments to show different web pages. Additionally, it details an application that stores user data using Shared Preferences. Each section includes code snippets for layout files and Java classes necessary for implementation.

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 display WhatsApp videos in grid view by using


Custom Adapter.
Step1: create below path inside Device Explorer folder
/storage/emulated/0/Android/media/[Link]/WhatsApp/Media/WhatsApp Video/
View
Tool Windows Device Explorer create above path
Steo2: copy .mp4 vodep file inside WhatsApp Video
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
item_video.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="[Link]
android:id="@+id/imgVideo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="100dp"
android:scaleType="centerCrop"/>

[Link]
package [Link];

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

import [Link];

SVFGC CTA 1
Android Programming

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

import [Link];
import
[Link];

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
ArrayList<File> videoList = new ArrayList<>();

private static final int REQUEST_CODE = 111;

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

recyclerView = findViewById([Link]);
[Link](new GridLayoutManager(this, 2));

checkPermission();
}

private void checkPermission()

{ String permission;

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


permission = [Link].READ_MEDIA_VIDEO;

SVFGC CTA 2
Android Programming

} else {
permission = [Link].READ_EXTERNAL_STORAGE;
}

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

readFiles();

} else {

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

@Override
public void
onRequestPermissionsResult( int
requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {

[Link]( req
uestCode,
permissions,
grantResults);

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

SVFGC CTA 3
Android Programming

readFiles();
}
}

private void readFiles() {

String path =
"/storage/emulated/0/Android/media/[Link]/WhatsApp/Media/WhatsApp Video/";

File folder = new File(path);

if (![Link]())
return;

File[] files = [Link]();

if (files == null)
return;

[Link]();

for (File file : files) {

if ([Link]()
&& ([Link]().endsWith(".mp4")
|| [Link]().endsWith(".3gp"))) {

[Link](file);
}
}

[Link](
new VideoAdapter(this, videoList));

SVFGC CTA 4
Android Programming

}
}

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

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


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

<application
android:allowBackup="true"
android:label="WhatsApp Videos"
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>
[Link]
package [Link];

import [Link];
import [Link];

SVFGC CTA 5
Android Programming

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

import [Link];
import [Link];

import [Link];
import [Link];

public class VideoAdapter


extends [Link]<[Link]> {

Context context;
ArrayList<File> files;

public VideoAdapter(Context context,


ArrayList<File> files) {

[Link] = context;
[Link] = files;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(
@NonNull ViewGroup parent,
int viewType) {

View view = [Link](context)

SVFGC CTA 6
Android Programming

.inflate(
[Link].item_video,
parent,
false);

return new ViewHolder(view);


}

@Override
public void
onBindViewHolder( @NonNull
ViewHolder holder, int position)
{

File file = [Link](position);

Bitmap bitmap = [Link](


[Link](),
[Link].MINI_KIND);

[Link](bitmap);
}

@Override
public int getItemCount()
{ return [Link]();
}

static class ViewHolder extends [Link]

{ ImageView imgVideo;

public ViewHolder(@NonNull View itemView) {


super(itemView);

SVFGC CTA 7
Android Programming

imgVideo = [Link]([Link]);
}
}
}

Output:

SVFGC CTA 8
Android Programming

2. Create an android application to display webpage by using Web view

Component. activity_main.xml

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


<WebView xmlns:android="[Link]
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

[Link]
package [Link];

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

import [Link];

public class MainActivity extends AppCompatActivity {

WebView webView;

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

webView = findViewById([Link]);

[Link](new WebViewClient());

[Link]().setJavaScriptEnabled(true);

SVFGC CTA 9
Android Programming

[Link]("[Link]
}
}

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

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

<application
android:allowBackup="true"
android:label="WebViewApp"
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>

SVFGC CTA 10
Android Programming

Output:

Enter URL or choose from the list shown above

SVFGC CTA 11
Android Programming

3. Create an android application to display different webpages in fragments by using


Fragments Component.
[Link]

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

import [Link];

public class CourseFragment extends Fragment

{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

return [Link](
[Link].fragment_courses,
container,
false);
}
}

[Link]

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

import [Link];

public class FacultyFragment extends Fragment

{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

return [Link](
[Link].fragment_faculty,
container,
false);
}
}

SVFGC CTA 12
Android Programming

[Link]

package [Link];

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

import [Link];

public class HomeFragment extends Fragment

{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{

return [Link](
[Link].fragment_home,
container,
false);
}
}

[Link]

package [Link];

import [Link];
import [Link];

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

public class MainActivity extends AppCompatActivity {

FragmentManager fm;

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

fm = getSupportFragmentManager();

[Link]()
.replace([Link].frag1, new HomeFragment())

SVFGC CTA 13
Android Programming

.commit();
}

public void home(View v) {


loadFragment(new HomeFragment());
}

public void courses(View v) {


loadFragment(new CourseFragment());
}

public void faculties(View v) {


loadFragment(new FacultyFragment());
}

private void loadFragment(Fragment fragment)


{ [Link]()
.replace([Link].frag1, fragment)
.commit();
}
}

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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:onClick="home"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Courses"
android:onClick="courses"/>

<Button

SVFGC CTA 14
Android Programming

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Faculty"
android:onClick="faculties"/>
</LinearLayout>

<FrameLayout
android:id="@+id/frag1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

Fragment_course.xml

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


<TextView
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Courses Fragment"
android:textSize="24sp"
android:gravity="center"/>

Fragment_faculty.xml

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


<TextView
xmlns:android="[Link]
" android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Faculty Fragment"
android:textSize="24sp"
android:gravity="center"/>

Fragment_home.xml

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


<TextView
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Welcome to Home Fragment"
android:textSize="24sp"
android:gravity="center"/>

SVFGC CTA 15
Android Programming

Output:

SVFGC CTA 16
Android Programming

4. Create an android application to store data by using Shared


Preferences. activity_main.xml

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


<FrameLayout xmlns:android="[Link]
android:id="@+id/flayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />

fragment_login.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="20dp">

<EditText
android:id="@+id/etUser"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Enter Username"/>

<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Enter Password"
android:inputType="textPassword"/>

<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:text="Save"/>

<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textSize="18sp"/>

</LinearLayout>

SVFGC CTA 17
Android Programming

[Link]
package [Link];

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

public class MainActivity extends AppCompatActivity {

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

FragmentManager fm = getFragmentManager();
FragmentTransaction tx = [Link]();

[Link]([Link], new LoginFragment());


[Link]();
}
}

[Link]

package [Link];

import

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

public class LoginFragment extends Fragment {

EditText etUser, etPass;


Button btnSave;
TextView tvResult;

SharedPreferences sp;

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,

SVFGC CTA 18
Android Programming

Bundle savedInstanceState) {

View v = [Link](
[Link].fragment_login,
container,
false);

etUser = [Link]([Link]);
etPass = [Link]([Link]);
btnSave = [Link]([Link]);
tvResult =
[Link]([Link]);

sp =
getActivity().getSharedPreferences( "
MyData",
getActivity().MODE_PRIVATE);

[Link](new [Link]()
{ @Override
public void onClick(View view) {

String user = [Link]().toString();


String pass = [Link]().toString();

[Link] editor = [Link]();

[Link]("username", user);
[Link]("password", pass);

[Link]();

[Link]("Data Saved Successfully");


}
});

return v;
}
}

[Link]

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


<manifest xmlns:android="[Link]
package="[Link]">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher
" android:label="@string/app_name"

SVFGC CTA 19
Android Programming

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 20
Android Programming

5. Create an android application to demonstrate concept of SQLite Database storage method

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

public class MainActivity extends AppCompatActivity {

EditText et1, et2, et3,


et4; SQLiteDatabase
dBase; ListView lview;

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

et1 =
findViewById([Link].et1); et2
= findViewById([Link].et2);
et3 =
findViewById([Link].et3); et4
= findViewById([Link].et4);

lview =

findViewById([Link]); dBase

= openOrCreateDatabase(
"empdb",
Context.MODE_PRIVATE,
null);

[Link](
"CREATE TABLE IF NOT EXISTS tbemp("
SVFGC CTA 21
Android Programming

+ "empid INTEGER PRIMARY KEY," +


"empname TEXT," +

SVFGC CTA 22
Android Programming

"empdesig TEXT," +
"empdept TEXT)");
}

public void insert(View v) {

ContentValues cv = new ContentValues();

[Link]("empid",
[Link]([Link]().toString()));
[Link]("empname",
[Link]().toString());
[Link]("empdesig",
[Link]().toString());
[Link]("empdept",
[Link]().toString());

long status =
[Link]( "tbemp",
null,
cv);

if(status != -1) {
[Link](
this,
"Data Inserted",
Toast.LENGTH_SHORT).show();

read(v);
} else {
[Link](
this,
"Insert Failed",
Toast.LENGTH_SHORT).show();
}
}

public void read(View v) {

Cursor c = [Link](
"tbemp"
, null,
null,
null,
null,
null,
null);

ArrayList<String> list =

SVFGC CTA 23
Android Programming

new ArrayList<>();

while([Link]()) {

String msg =
[Link](0) + " | " +
[Link](1) + " | " +
[Link](2) + " | " +
[Link](3);

[Link](msg);
}

[Link]();

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

[Link](adapter);
}

public void update(View v) {

ContentValues cv = new ContentValues();

[Link]("empname",
[Link]().toString());
[Link]("empdesig",
[Link]().toString());
[Link]("empdept",
[Link]().toString());

int status =
[Link]( "tbe
mp",
cv,
"empid=?",
new String[]{
[Link]().toString()
});

if(status > 0) {
[Link](
this,
"Updated Successfully",
Toast.LENGTH_SHORT).show();

SVFGC CTA 24
Android Programming

read(v);
} else {
[Link](
this,
"Update Failed",
Toast.LENGTH_SHORT).show();
}
}

public void delete(View v)

{ int status =

[Link](
"tbemp",
"empid=?",
new String[]{
[Link]().toString()
});

if(status > 0) {
[Link](
this,
"Deleted Successfully",
Toast.LENGTH_SHORT).show();

read(v);
} else {
[Link](
this,
"Delete Failed",
Toast.LENGTH_SHORT).show();
}
}

SVFGC CTA 25
Android Programming

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">

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee ID"
android:inputType="number"/>

<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Name"/>

<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Designation"/>

<EditText
android:id="@+id/et4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Department"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="insert"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"

SVFGC CTA 26
Android Programming

android:onClick="read"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="update"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="delete"/>
</LinearLayout>

<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

</LinearLayout>

Output:

SVFGC CTA 27
Android Programming

SVFGC CTA 28
Android Programming

6. Write an android program to develop Video view application

Step 1: Create raw Folder

Create:

app
└─ src
└─ main
└─ res
└─ raw
└─ sample.mp4

Copy any MP4 video and rename it sample.mp4.

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">

<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp" />

</LinearLayout>

SVFGC CTA 29
Android Programming

[Link]
package [Link];

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

import [Link];

public class MainActivity extends AppCompatActivity {

VideoView videoView;

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

videoView = findViewById([Link]);

String path =
"[Link]://" +
getPackageName() +
"/" +
[Link];

Uri uri = [Link](path);

[Link](uri);

MediaController mediaController =
new MediaController(this);

[Link](videoView);

[Link](
mediaController);

[Link]();
}

SVFGC CTA 30
Android Programming

Output:

SVFGC CTA 31
Android Programming

PART B
1. Create an android application to perform different types of operations (Send SMS, Making
call and sending email) by using Telephony app.
activity_main.xml

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Phone Number" />

<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent
" android:layout_height="48dp"
android:hint="Message" />

<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Email Address" />

<Button
android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Send SMS" />

<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Make Call" />

<Button
android:id="@+id/btnEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Send Email" />

</LinearLayout>

SVFGC CTA 32
Android Programming

[Link]

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


<manifest xmlns:android="[Link]

<application
android:allowBackup="true"
android:label="TelephonyApp"
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>

[Link]
package [Link];

import static [Link].R.*;

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

import [Link];

public class MainActivity extends AppCompatActivity {

EditText etPhone, etMessage, etEmail;


Button btnSms, btnCall, btnEmail;

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

etPhone = findViewById([Link]);

SVFGC CTA 33
Android Programming

etMessage = findViewById([Link]);
etEmail = findViewById([Link]);
btnSms = findViewById([Link]);
btnCall = findViewById([Link]);
btnEmail = findViewById([Link]);

[Link](v -> {

Intent intent = new Intent(Intent.ACTION_VIEW);


[Link]([Link]("[Link] + [Link]().toString()));
[Link]("sms_body", [Link]().toString());

startActivity(intent);
});

[Link](v -> {

Intent intent = new Intent(


Intent.ACTION_DIAL,
[Link]("[Link] + [Link]().toString()));

startActivity(intent);
});

[Link](v -> {

Intent intent = new

Intent(Intent.ACTION_SEND);

[Link]("message/rfc822");

[Link](
Intent.EXTRA_EMAIL,
new String[]{[Link]().toString()});

[Link](
Intent.EXTRA_SUBJECT,
"Test Mail");

[Link](
Intent.EXTRA_TEXT,
"Hello from Android Application");

startActivity(
[Link](
intent,
"Select Email App"));
});
}
}

SVFGC CTA 34
Android Programming

Output:

SVFGC CTA 35
Android Programming

SVFGC CTA 36
Android Programming

2. Write an android program to develop Media player


application. Step 1:
app/
└── java/
└── [Link]/
└── [Link]

└── res/
├── layout/activity_main.xml
└── raw/song.mp3 (IMPORTANT)
Step2:

Step3:

SVFGC CTA 37
Android Programming

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Background Image -->


<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg_music"
android:scaleType="centerCrop" />

<!-- Foreground Controls -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp"
android:background="#80000000">

<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="#FFFFFF"/>

<Button
android:id="@+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"

SVFGC CTA 38
Android Programming

android:text="Pause"
android:textColor="#FFFFFF"/>

<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:textColor="#FFFFFF"/>

</LinearLayout>

</FrameLayout>

[Link]
package [Link];

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

import [Link];

public class MainActivity extends AppCompatActivity {

MediaPlayer mediaPlayer;
Button btnPlay, btnPause, btnStop;

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

SVFGC CTA 39
Android Programming

setContentView([Link].activity_main);

btnPlay = findViewById([Link]);
btnPause = findViewById([Link]);
btnStop = findViewById([Link]);

mediaPlayer = [Link](this, [Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v)
{ if
([Link]()) {
[Link]();
}
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]();
mediaPlayer = [Link]([Link], [Link]);
}
});
}

SVFGC CTA 40
Android Programming

@Override
protected void onDestroy() {
[Link]();
if (mediaPlayer != null) {
[Link]();
}
}
}

Output:

SVFGC CTA 41
Android Programming

3. Create an android application to display current location on Google maps by using


Google-Maps Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="[Link]
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
[Link]
package [Link];

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

import [Link];

public class MainActivity extends AppCompatActivity {

WebView webView;

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

webView = findViewById([Link]);

[Link](new WebViewClient());

String lat = "14.4644"; // example: Davangere latitude


String lon = "75.9218"; // example: Davangere longitude

SVFGC CTA 42
Android Programming

String url =
"[Link]
+ lat + "," + lon + ",15z";

[Link](url);
}
}
[Link]
<manifest xmlns:android="[Link]
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:label="Map Image App"
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 43
Android Programming

4. Write an android program to develop Audio Recording


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

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

<application
android:allowBackup="true"
android:label="AudioRecorder"
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>

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

SVFGC CTA 44
Android Programming

import [Link];

public class MainActivity extends AppCompatActivity

{ Button btnRecord, btnStop, btnPlay;

MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;

String fileName;

private static final int REQUEST_PERMISSION_CODE = 1000;

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

btnRecord = findViewById([Link]);
btnStop = findViewById([Link]);
btnPlay = findViewById([Link]);

fileName = getExternalFilesDir(null).getAbsolutePath()
+ "/recorded_audio.3gp";

if (!checkPermissions()) {
requestPermissions();
}

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

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

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


}

private void startRecording()

{ mediaRecorder = new MediaRecorder();

[Link]([Link]);
[Link]([Link].THREE_GPP);
[Link]([Link].AMR_NB);
[Link](fileName);

try {
[Link]();
[Link]();
[Link](this,

SVFGC CTA 45
Android Programming

"Recording Started",
Toast.LENGTH_SHORT).show();

} catch (IOException e) {
[Link]();
}
}

private void stopRecording()

{ if (mediaRecorder != null)

{
[Link]();
[Link]();
mediaRecorder = null;

[Link](this,
"Recording Saved",
Toast.LENGTH_SHORT).show();
}
}

private void playRecording()

{ mediaPlayer = new

MediaPlayer(); try {
[Link](fileName);
[Link]();
[Link]();

[Link](this,
"Playing Audio",
Toast.LENGTH_SHORT).show();

} catch (IOException e) {
[Link]();
}
}

private boolean checkPermissions() {


return [Link](
this,
[Link].RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED;
}

private void requestPermissions() {


[Link](
this,

SVFGC CTA 46
Android Programming

new String[]{
[Link].RECORD_AUDIO
},
REQUEST_PERMISSION_CODE);
}

@Override
public void
onRequestPermissionsResult( int
requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {

[Link](
requestCode,
permissions,
grantResults);

if (requestCode == REQUEST_PERMISSION_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();
}
}
}
}

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

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

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

SVFGC CTA 47
Android Programming

<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 48
Android Programming

5. Write an android program to develop Video Recording


application. 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:gravity="center"
android:padding="16dp">

<Button
android:id="@+id/btnRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record Video"/>

<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"/>

</LinearLayout>

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

{ Button btnRecord;
VideoView videoView;

ActivityResultLauncher<Intent> videoLauncher;

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

SVFGC CTA 49
Android Programming

btnRecord = findViewById([Link]);
videoView = findViewById([Link]);

videoLauncher = registerForActivityResult(
new [Link](),
result -> {
if ([Link]() == RESULT_OK
&& [Link]() != null) {

Uri videoUri = [Link]().getData();

[Link](videoUri);

MediaController mediaController =
new MediaController(this);

[Link](videoView);

[Link](mediaController);

[Link]();
}
});

[Link](v ->

{ Intent intent =
new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

[Link](intent);
});
}
}

[Link]
<manifest xmlns:android="[Link]

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

<uses-feature
android:name="[Link]"
android:required="false" />

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

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

SVFGC CTA 50
Android Programming

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

</activity>

</application>

</manifest>

[Link] (Module: app)

Ensure these dependencies exist:

dependencies {

implementation '[Link]:appcompat:1.7.0'
implementation '[Link]:material:1.12.0'

Output:

SVFGC CTA 51
Android Programming

6. Write an android program to develop Camera and Gallery applications.

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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:onClick="camera"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:onClick="gallery"/>

<ImageView
android:id="@+id/ivew1"
android:layout_width="250dp"
android:layout_height="250dp" />

</LinearLayout>

[Link]

package [Link];

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

public class MainActivity extends AppCompatActivity {

ImageView iview;

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

SVFGC CTA 52
Android Programming

iview = findViewById([Link].ivew1);
}

public void camera(View v) {


Intent i = new Intent("[Link].IMAGE_CAPTURE");
startActivityForResult(i, 111);
}

public void gallery(View v)


{ Intent i = new Intent();
[Link](Intent.ACTION_GET_CONTENT);
[Link]("image/*");
startActivityForResult(i, 123);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);

if (resultCode == RESULT_OK && data != null)

{ if (requestCode == 111) {
Bitmap bmp = (Bitmap) [Link]().get("data");
[Link](bmp);
}

if (requestCode == 123)
{ Uri u = [Link]();
[Link](u);
}
}
}
}

[Link]

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


<manifest xmlns:android="[Link]

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

<uses-feature
android:name="[Link]"
android:required="false"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher
"

SVFGC CTA 53
Android Programming

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">

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

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

</activity>

</application>

</manifest>

Output:

SVFGC CTA 54
Android Programming

7. Create an android application to get latitude and longitude value by using Location 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/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:textSize="30sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude"
android:textSize="30sp"
android:textStyle="bold"
android:layout_below="@id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>

</RelativeLayout>

[Link]
package [Link];

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

import [Link];

public class MainActivity extends AppCompatActivity {

SVFGC CTA 55
Android Programming

TextView tv1, tv2;

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

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

LocationManager lManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);

if (checkSelfPermission([Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
checkSelfPermission([Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

requestPermissions(
new String[]{
[Link].ACCESS_FINE_LOCATION,
[Link].ACCESS_COARSE_LOCATION
},
100);
return;
}

[Link]( LocationMan
ager.NETWORK_PROVIDER, 1000,
1,
new LocationListener()
{ @Override
public void onLocationChanged(Location location) {

double lati = [Link]();


double longi = [Link]();

[Link]("Latitude : " + lati);


[Link]("Longitude : " +
} longi);
});
}
}

SVFGC CTA 56
Android Programming

[Link]

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


<manifest xmlns:android="[Link]

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

<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">

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

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

</activity>

</application>

</manifest>

[Link]

fun implementation(string: String) {}

dependencies {
implementation("[Link]:play-services-location:21.3.0")
}

Output:

SVFGC CTA 57
Android Programming

8. 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 58
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 59
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 60
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 61
Android Programming

[Link](

SVFGC CTA 62
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 63
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 64
Android Programming

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

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

</activity>

</application>

</manifest>

Output:

SVFGC CTA 65
Android Programming

9. 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 66
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 67
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 68
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 69
Android Programming

10. 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 70
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 71
Android Programming

setContentView([Link].activity_main);

SVFGC CTA 72
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 73
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 74
Android Programming

REQUEST_CODE);
}

SVFGC CTA 75
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 76
Android Programming

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

SVFGC CTA 77
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 78
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 79
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 80
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 81
Android Programming

android:exported="true">

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

</activity>

</application>

</manifest>

Output:

SVFGC CTA 82
Android Programming

11. 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 83
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 84
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 85
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 86
Android Programming

12. 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 87
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 88
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 89
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 90
Android Programming

SVFGC CTA 91
Android Programming

Output:

SVFGC CTA 92

You might also like