0% found this document useful (0 votes)
2 views

Module 5

Module 5 provides a guide on sending SMS and email in Android using Kotlin. It details the steps for requesting permissions, creating UI elements, and implementing code for sending SMS via SmsManager and sending emails using intents. The document includes sample code for both functionalities and emphasizes the need for user permissions and proper intent handling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 5

Module 5 provides a guide on sending SMS and email in Android using Kotlin. It details the steps for requesting permissions, creating UI elements, and implementing code for sending SMS via SmsManager and sending emails using intents. The document includes sample code for both functionalities and emphasizes the need for user permissions and proper intent handling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

‭Module 5‬

‭1. SMS Messaging‬

‭ o‬ ‭send‬ ‭SMS‬ ‭in‬ ‭an‬ ‭Android‬ ‭Kotlin‬ ‭program,‬ ‭you‬ ‭will‬ ‭use‬ ‭the‬ ‭SmsManager‬ ‭class,‬ ‭which‬
T
‭allows‬ ‭you‬‭to‬‭send‬‭text‬‭messages‬‭directly‬‭from‬‭your‬‭app.‬‭Below‬‭is‬‭a‬‭step-by-step‬‭guide‬‭and‬
‭code implementation.‬

‭Step 1. Request Permissions:‬

‭ ince‬ ‭sending‬ ‭SMS‬ ‭requires‬ ‭explicit‬ ‭user‬ ‭permission,‬ ‭you‬ ‭need‬ ‭to‬ ‭add‬ ‭the‬ ‭necessary‬
S
‭permission to the AndroidManifest.xml and request runtime permissions in your activity.‬

‭In AndroidManifest.xml, add the SEND_SMS permission and Telephony Hardware feature:‬

‭‭
<u‬ses-permission‬‭
android‬
:name‬
‭ ="android.permission.SEND_SMS"‬
‭ />‬

<‭
‭u‬ses-feature‬‭
android‬
:name‬
‭ ="android.hardware.telephony"‬
‭ />‬

‭Step 2. Create a UI Design to get Phone number and Message:‬

I‭ n your activity_main.xml, add an EditText to enter the phone number, another EditText for‬
‭the message, and a Button to send the SMS.‬

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

<‭
‭L‬inearLayout‬
android‬
‭ :orientation‬
‭ ="vertical"‬

android‬
‭ :padding‬
‭ ="16dp"‬

>‬

<‭
‭T‬extView‬
android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Sending SMS App"‬

android‬
‭ :textColor‬
‭ ="#D81B60"‬

android‬
‭ :textSize‬
‭ ="26dp"‬

android‬
‭ :layout_marginTop‬
‭ ="50dp"‬

android‬
‭ :gravity‬
‭ ="center_horizontal"‬

/>‬

<‭
‭E‬ditText‬
android‬
‭ :id‬
‭ ="@+id/phonenumber"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :hint‬
‭ ="Enter Phone Number"‬

android‬
‭ :inputType‬
‭ ="phone"‬

android‬
‭ :layout_marginTop‬
‭ ="50dp"‬

/>‬

<‭
‭E‬ditText‬
‭ndroid‬
a :id‬
‭ ="@+id/messageinput"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :hint‬
‭ ="Enter Message"‬

android‬
‭ :inputType‬
‭ ="textMultiLine"‬
‭ />‬

<‭
‭B‬utton‬
android‬
‭ :id‬
‭ ="@+id/sendbtn"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Send SMS"‬

android‬
‭ :layout_marginTop‬
‭ ="16dp"‬‭
‭ />‬

</‬
‭ LinearLayout‬
‭ >‬

‭Step 3. Implement MainActivity.kt Code for sending SMS message‬

I‭ n‬ ‭your‬ ‭MainActivity.kt,‬ ‭you‬ ‭will‬ ‭need‬ ‭to‬ ‭check‬ ‭for‬ ‭runtime‬ ‭permissions‬ ‭and‬ ‭use‬ ‭the‬
‭SmsManager to send an SMS when the button is clicked‬

‭mport‬‭
i android.‬
Manifest‬

import‬‭
‭ android.content.pm.‬PackageManager‬

import‬‭
‭ android.os.‬
Bundle‬

import‬‭
‭ android.telephony.‬SmsManager‬

import‬‭
‭ android.widget.‬
Button‬

import‬‭
‭ android.widget.‬
EditText‬

import‬‭
‭ android.widget.‬
Toast‬

import‬‭
‭ androidx.activity.enableEdgeToEdge‬
import‬‭
‭ androidx.appcompat.app.‬AppCompatActivity‬

import‬‭
‭ androidx.core.app.‬ActivityCompat‬

class‬‭
‭ MainActivity‬‭
: AppCompatActivity() {‬
override fun‬‭
‭ onCreate‬(‭
‭s‬avedInstanceState‬:‬‭
‭ Bundle‬
?) {‬

super‬
‭ .onCreate(‬
‭ savedInstanceState‬
‭ )‬

enableEdgeToEdge‬
‭ ()‬

setContentView(‬
‭ R‭
‭.‬‬
layout‬
‭ .‭
‭a‬ctivity_main‬)‬

‭al‬‭
v phonefield‬:‬
‭ EditText‬‭
‭ = findViewById(‬ R‭
‭.‬‬
id‬
‭ .‭
‭p‬honenumber‬)‬

val‬‭
‭ msgfield‬
:‬
‭ EditText‬‭
‭ = findViewById(‬ R‭
‭.‬‬
id‬
‭ .‭
‭m‬essageinput‬)‬

val‬‭
‭ sendbtn‬
:‭
‭B‬utton‬‭= findViewById(‬
R‬
‭ .‭
‭i‬d‬
.‬
‭ sendbtn‬
‭ )‬

sendbtn‬
‭ .setOnClickListener‬‭
‭ {‬
//Request Runtime Permission to SEND_SMS‬

‭f‬
i (‭
‭A
‬ctivityCompat‬.checkSelfPermission(‬
‭ this‬
‭ ,‬
‭ Manifest‬
‭ .‭
‭p‬ermission‬
.‬
‭ SEND_‬

SMS‬
‭ )!=‬
‭ PackageManager‬
‭ .‬
‭ PERMISSION_GRANTED‬
‭ ){‬

ActivityCompat‬
‭ .requestPermissions(‬
‭ this‬
‭ ,‬

arrayOf‬
‭ (‭
‭M
‬anifest‬
.‬
‭ permission‬
‭ .‭
‭S‬END_SMS‬
),‬
‭ 1‬
‭ )‬

}‬

‭ar‬‭
v phonenumber‬‭=‬‭
phonefield‬.getText().toString()‬

var‬‭
‭ msg‬‭
=‬‭
msgfield‬.getText().toString()‬

if‬
‭ (‬
‭ phonenumber‬
‭ .‬
‭ isNotEmpty‬
‭ () &&‬‭
‭ msg‬
.‬
‭ isNotEmpty‬
‭ ()){‬

val‬‭
‭ smsManager‬‭ =‬‭
SmsManager‬
.getDefault()‬

smsManager‬
‭ .sendTextMessage(‬
‭ phonenumber‬
‭ ,‬
‭ null‬
‭ ,‭
‭m‬sg‬
,‭
‭n‬ull‬
,‬
‭ null‬
‭ )‬

Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‭
‭"‬SMS Send‬
Successfully"‬
‭ ,‭
‭T‬oast‬
.‭
‭L‬ENGTH_LONG‬).show()‬

}‬

else‬
‭ {‬

Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‭
‭"‬Please Enter Phone Number and‬
Message"‬
‭ ,‬
‭ Toast‬
‭ .‬
‭ LENGTH_LONG‬
‭ ).show()‬

}‬

}}}‬

‭Open Messages in your emulator‬
‭2. Sending Email‬

I‭ n Android, sending an email can be achieved by using intents. An intent allows your app to‬
‭interact with other apps on the device. In the case of sending an email, we use an implicit‬
‭intent to launch an email client installed on the user’s device.‬

I‭ ntent.ACTION_SEND‬‭is used specifically for sending‬‭emails which ensures that only apps‬
‭that handle email (and not other sharing services) will respond to the intent.‬

val‬‭
‭ intent‬‭
= Intent(‬
Intent‬
‭ .‬
‭ ACTION_SEND‬
‭ )‬

‭Components of Email Intent‬


‭●‬ ‭Intent Data:‬
‭Uri.parse("mailto:") — This ensures that only email-capable apps respond to‬
‭the intent.‬

‭●‬ ‭Intent Extras:‬


‭These key-value pairs provide additional data for the intent:‬
‭Intent.EXTRA_EMAIL: An array of email addresses (recipients).‬
‭Intent.EXTRA_SUBJECT: The subject of the email.‬
‭Intent.EXTRA_TEXT: The body of the email message.‬

‭Sample Code for Creating Email Intent‬

val‬‭
‭ emailIntent‬‭
= Intent(‬Intent‬
‭ .‭
‭A‬CTION_SENDTO‬).‬
‭ apply‬‭
‭ {‬
data‬‭
‭ =‬‭
Uri‬
.parse(‬
‭ "mailto:"‬
‭ )‬

type‬‭
‭ =‬‭
"message/rfc822"‬
putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_EMAIL‬,‬‭
‭ arrayOf‬
(‬
‭ "[email protected]"‬
‭ ))‬

putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_SUBJECT‬ ,‬‭
‭ "Subject of the‬‭email"‬
)‬

putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_TEXT‬
,‬‭
‭ "Body of the email"‬ )‬

}‬

‭Add Extra Information‬‭:‬

‭●‬ ‭ dd the recipient email address, subject, and message body using the‬‭
A putExtra()‬
‭method.‬

‭Sending Email Intent‬

‭Use‬‭startActivity()‬‭to launch an email app that can‬‭handle the intent.‬

startActivity(‬
‭ Intent‬
‭ .createChooser(‬
‭ intent‬
‭ ,‭
‭"‬Choose‬‭
Email App.."‬
))‬

‭Example Program: Sending an Email Message using Intent‬

‭Step 1: Create a New Project‬

1‭ .‬ O‭ pen‬‭Android Studio‬‭and create a‬‭New Project‬‭.‬


‭2.‬ ‭Choose‬‭Empty Views Activity‬‭.‬
‭3.‬ ‭Name‬ ‭your‬ ‭project‬ ‭(e.g.,‬ ‭"SendEMail")‬ ‭and‬ ‭make‬ ‭sure‬ ‭to‬ ‭select‬ ‭Kotlin‬ ‭as‬ ‭the‬
‭language.‬

‭Step 2: Add Permissions (Optional)‬

‭ ny‬ ‭special‬ ‭permissions‬ ‭are‬ ‭not‬ ‭needed‬ ‭for‬ ‭sending‬ ‭emails‬ ‭using‬ ‭intents.‬ ‭However,‬ ‭if‬
A
‭internet-based‬ ‭services‬ ‭are‬ ‭used‬ ‭later‬ ‭on,‬ ‭the‬ ‭Internet‬ ‭permission‬ ‭should‬ ‭be‬ ‭needed‬ ‭in‬ ‭the‬
AndroidManifest.xml‬‭file.‬

‭<uses-permission android:name="android.permission.INTERNET" />‬


‭Step 3: Create the UI‬
I‭ n the‬‭activity_main.xml‬‭layout file, create a simple‬‭UI with a few input fields to get email‬
‭details like the recipient's email address, subject, and message.‬
<?‬
‭ xml version‬
‭ ="1.0"‬‭
‭ encoding‬ ="utf-8"‬
‭ ?>‬

<‭
‭L‬inearLayout‬
android‬
‭ :id‬
‭ ="@+id/main"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

android‬
‭ :orientation‬
‭ ="vertical"‬

>‬

<‭
‭T‬extView‬
android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Sending Email"‬

/>‬

<‭
‭E‬ditText‬
android‬
‭ :id‬
‭ ="@+id/toemail"‬

android‬
‭ :layout_width‬
‭ ="300dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :hint‬
‭ ="To Email.."‬

/>‬

<‭
‭E‬ditText‬
android‬
‭ :id‬
‭ ="@+id/subject"‬

android‬
‭ :layout_width‬
‭ ="300dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :hint‬
‭ ="Subject"‬

/>‬

<‭
‭E‬ditText‬
android‬
‭ :id‬
‭ ="@+id/message"‬

‭ndroid‬
a :layout_width‬
‭ ="300dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :hint‬
‭ ="Message"‬

android‬
‭ :inputType‬
‭ ="textMultiLine"‬

/>‬

<‭
‭B‬utton‬
android‬
‭ :id‬
‭ ="@+id/sendbtn"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Send Email"‬

/>‬

</‬
‭ LinearLayout‬
‭ >‬

‭Step 4: Kotlin Code in Activity‬


I‭ n the main activity, write the Kotlin code to handle the email sending functionality.‬
class‬‭
‭ MainActivity‬‭ : AppCompatActivity() {‬
override fun‬‭
‭ onCreate‬ (‭
‭s‬avedInstanceState‬ :‬‭
‭ Bundle‬ ?)‬‭
‭ {‬
super‬
‭ .onCreate(‬
‭ savedInstanceState‬
‭ )‬

enableEdgeToEdge‬
‭ ()‬

setContentView(‬
‭ R‭
‭.‬‬
layout‬
‭ .‭
‭ a
‬ctivity_main‬ )‬

‭al‬‭
v toemail‬
:‭
‭E‬ditText‬‭
= findViewById(‬R‬
‭ .‭
‭i‬d‬
.‬
‭ toemail‬
‭ )‬

val‬‭
‭ subject‬
:‭
‭E‬ditText‬‭
= findViewById(‬R‬
‭ .‭
‭i‬d‬
.‬
‭ subject‬
‭ )‬

val‬‭
‭ message‬
:‭
‭E‬ditText‬‭
= findViewById(‬R‬
‭ .‭
‭i‬d‬
.‬
‭ message‬
‭ )‬

val‬‭
‭ sendbtn‬
:‭
‭B‬utton‬‭
= findViewById(‬
R‬
‭ .‭
‭i‬d‬
.‬
‭ sendbtn‬
‭ )‬

sendbtn‬
‭ .setOnClickListener‬‭
‭ {‬
val‬‭
‭ intent‬‭= Intent(‬Intent‬
‭ .‬
‭ ACTION_SEND‬
‭ ).‬
‭ apply‬‭
‭ {‬
data‬‭
‭ =‬‭
Uri‬
.parse(‬
‭ "mailto:"‬
‭ )‬

putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_EMAIL‬,‬‭
‭ arrayOf‬(‬
‭ toemail‬
‭ .‬
‭ text‬
‭ .toString()))‬

type‬‭
‭ =‬‭
"message/rfc822"‬
putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_SUBJECT‬ ,‬

subject‬
‭ .‭
‭t
‬ext‬.toString())‬

putExtra(‬
‭ Intent‬
‭ .‭
‭E‬XTRA_TEXT‬,‬‭
‭ message‬.‭
‭t‬ext‬.toString())‬

}‬

startActivity(‬
‭ Intent‬
‭ .createChooser(‬
‭ intent‬
‭ ,‭
‭"‬Choose‬‭
Email‬
App.."‬
‭ ))‬

}‬

}‬

}‬

‭Step 5: Explanation of the Code‬


‭1.‬ U ‭ I Elements‬‭: The user inputs the recipient’s email address, subject, and message in‬
‭the respective‬‭EditText‬‭fields. When the "Send Email"‬‭button is pressed, the‬
buttonSend.setOnClickListener { ... }‬‭is triggered.‬

‭2.‬ ‭Intent Handling‬‭: The‬‭Intent‬‭is used to request the email app on the user's phone.‬
‭The‬‭ACTION_SEND‬‭action is used here.‬
o‬ ‭
‭ putExtra(Intent.EXTRA_EMAIL, arrayOf(to))‬ ‭: Adds the‬‭recipient email.‬
‭‬ ‭
o putExtra(Intent.EXTRA_SUBJECT, subject)‬ ‭: Adds the‬‭subject line.‬
o‬ ‭
‭ putExtra(Intent.EXTRA_TEXT, message)‬ ‭: Adds the email‬‭body message.‬
‭3.‬ ‭ tarting the Intent‬‭: Once the intent is ready,‬‭startActivity(intent)‬‭is called. If‬
S
t‭here’s an email app available, Android will open it, and the email will be prefilled‬
‭with the subject, recipient, and message.‬
‭ .‬ ‭message/rfc822‬‭MIME Type‬‭: This ensures that the apps‬‭offered to handle the intent‬
4
‭are restricted to email clients only, such as Gmail, Outlook, etc.‬
‭5.‬ ‭Handling Empty Fields‬‭: A simple validation is added‬‭to ensure the email, subject,‬
‭and message fields are not empty before sending the intent.‬
‭3. Accessing Camera‬
‭ ccessing‬ ‭the‬ ‭camera‬ ‭in‬ ‭Android‬‭can‬‭be‬‭achieved‬‭using‬‭intent.‬‭This‬‭approach‬‭launches‬‭the‬
A
‭built-in camera app, captures a photo, and returns the result to your activity.‬
‭We use‬‭MediaStore.ACTION_IMAGE_CAPTURE‬‭to launch the‬‭default camera app.‬
‭Step 1:‬‭Create a New Android Project‬
‭Step 2:‬‭Add permissions to the‬‭Android Manifest‬‭: To‬‭access the camera and store the‬
‭image, add the following permissions in your AndroidManifest.xml file.‬
‭To allow the app to access the camera:-‬
<‭
‭u‬ses-permission‬‭ android‬ :name‬
‭ ="android.permission.CAMERA"‬
‭ />‬

‭ his ensures that the app can only be installed on devices that have a camera:-‬
T
<‭
‭ u
‬ses-feature‬‭ android‬ :name‬
‭ ="android.hardware.camera"‬
‭ />‬

‭ o allow the app to write and read the image files from external storage.‬
T
<‭
‭ u
‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.WRITE_EXTERNAL_STORAGE"‬
‭ />‬

<‭
‭ u
‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.READ_EXTERNAL_STORAGE"‬
‭ />‬

‭Also,‬
‭●‬ ‭FileProvider:‬‭You need to define a‬‭FileProvider‬‭in‬‭the‬‭AndroidManifest.xml‬‭to‬
‭securely share the image file between the android app and the camera app. The file‬
‭paths are defined in a separate XML file (‬‭file_paths.xml)‬‭inside res/xml directory‬

<‭
‭m‬anifest‬>‬

<‭
‭u‬ses-permission‬‭android‬
:name‬
‭ ="android.permission.CAMERA"‬
‭ />‬

<‭
‭u‬ses-feature‬‭android‬
:name‬
‭ ="android.hardware.camera"‬
‭ />‬

<‭
‭u‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.WRITE_EXTERNAL_STORAGE"‬
‭ />‬

<‭
‭u‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.READ_EXTERNAL_STORAGE"‬
‭ />‬

<‭
‭a‬pplication‬
<‬
‭ activity>‬

…‬

</‬
‭ activity‬
‭ >‬

<‬
‭ provider‬

android‬
‭ :name‬
‭ ="androidx.core.content.FileProvider"‬

android‬
‭ :authorities‬
‭ ="${applicationId}.provider"‬

android‬
‭ :exported‬
‭ ="false"‬

android‬
‭ :grantUriPermissions‬
‭ ="true"‬
‭ >‬

<‬
‭ meta-data‬

android‬
‭ :name‬
‭ ="android.support.FILE_PROVIDER_PATHS"‬

android‬
‭ :resource‬
‭ ="@xml/file_paths"‬‭
‭ />‬
</‬
‭ provider‬
‭ >‬

</‬
‭ application‬
‭ >‬

</‬
‭ manifest‬
‭ >‬

‭ tep 3:‬‭Define the‬‭file paths‬‭in res/xml/file_paths.xml:‬
S
‭?‬
< xml version‬
‭ ="1.0"‬‭
‭ encoding‬ ="utf-8"‬
‭ ?>‬

<‭
‭ p
‬aths‬‭ xmlns:‬ android‬
‭ ="http://schemas.android.com/apk/res/android"‬
‭ >‬

<‭
‭ e
‬xternal-path‬‭ name‬ ="images"‬‭
‭ path‬ ="."‬‭
‭ />‬
</‬
‭ paths‬
‭ >‬

‭In Android,‬‭ <‬external-path>‬‭refers to the shared storage‬‭space where files can be stored‬

‭and accessed by all applications. This storage is often represented as the‬‭/storage/emulated/0/‬
‭directory on devices.‬
‭Step 4: Design a Simple UI for Taking Photo from camera‬
<?‬
‭ xml version‬
‭ ="1.0"‬‭
‭ encoding‬ ="utf-8"‬
‭ ?>‬

<‬
‭ LinearLayout‬

xmlns:‬
‭ android‬
‭ ="http://schemas.android.com/apk/res/android"‬

xmlns:‬
‭ app‬
‭ ="http://schemas.android.com/apk/res-auto"‬

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

android‬
‭ :id‬
‭ ="@+id/main"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

tools‬
‭ :context‬
‭ =".MainActivity"‬

android‬
‭ :orientation‬
‭ ="vertical"‬

>‬

<‬
‭ Button‬

android‬
‭ :id‬
‭ ="@+id/btn"‬

android‬
‭ :layout_width‬
‭ ="200dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Take Photo"‬

android‬
‭ :layout_marginTop‬
‭ ="50dp"‬

android‬
‭ :layout_gravity‬
‭ ="center_horizontal"‬

/>‬

<‬
‭ ImageView‬

android‬
‭ :id‬
‭ ="@+id/imageView"‬

android‬
‭ :layout_width‬
‭ ="300dp"‬

android‬
‭ :layout_height‬
‭ ="400dp"‬

tools‬
‭ :srcCompat‬
‭ ="@tools:sample/avatars"‬

android‬
‭ :layout_marginTop‬
‭ ="20dp"‬

android‬
‭ :layout_gravity‬
‭ ="center_horizontal"‬

/>‬

</‬
‭ LinearLayout‬
‭ >‬

‭Step 5: Implement the MainActivity.kt code to request permission and take photo.‬
package‬‭
‭ com.example.cameraaccess‬

‭mport‬‭
i android.‬
Manifest‬

import‬‭
‭ android.content.‬
Intent‬

import‬‭
‭ android.content.pm.‬PackageManager‬

import‬‭
‭ android.graphics.‬BitmapFactory‬

import‬‭
‭ android.net.‬
Uri‬

‭mport‬‭
i android.os.‬
Build‬

import‬‭
‭ android.os.‬
Bundle‬

import‬‭
‭ android.os.‬
Environment‬

import‬‭
‭ android.provider.‬MediaStore‬

import‬‭
‭ android.widget.‬
Button‬

import‬‭
‭ android.widget.‬
ImageView‬

import‬‭
‭ android.widget.‬
Toast‬

import‬‭
‭ androidx.activity.result.‬ ActivityResultLauncher‬

import‬‭
‭ androidx.activity.result.contract.‬ ActivityResultContracts‬

import‬‭
‭ androidx.appcompat.app.‬ AppCompatActivity‬

import‬‭
‭ androidx.core.app.‬ActivityCompat‬

import‬‭
‭ androidx.core.content.‬FileProvider‬

import‬‭
‭ java.io.‬
File‬

import‬‭
‭ java.text.‬
SimpleDateFormat‬

import‬‭
‭ java.util.‬
Date‬

class‬‭
‭ MainActivity‬‭
: AppCompatActivity() {‬

‭rivate
p lateinit var‬‭
imageView‬
:‬‭
‭ ImageView‬
private
‭ lateinit var‬‭
btnCapture‬:‬‭
‭ Button‬
private
‭ lateinit var‬‭
photoUri‬
:‬‭
‭ Uri‬
private
‭ var‬‭
photoFile‬
:‬‭
‭ File‬
? =‬‭
‭ null‬

‭rivate val‬‭
p cameraLauncher‬ :‬‭
‭ ActivityResultLauncher‬<‬
‭ Intent‬
‭ >‬‭
‭ =‬
registerForActivityResult(‬
‭ ActivityResultContracts‬
‭ .StartActivityForResult())‬

{‬‭
‭ result‬‭ ->‬
if‬‭
‭ (‭
r
‬esult‬.‬
‭ resultCode‬‭
‭ ==‬‭
RESULT_OK‬‭
&&‬‭
photoFile‬‭ !=‬‭
null‬ ) {‬

val‬‭
‭ bitmap‬‭
=‬‭
BitmapFactory‬.decodeFile(‬
‭ photoFile‬
‭ !!.‬
‭ absolutePath‬
‭ )‬

imageView‬
‭ .setImageBitmap(‬
‭ bitmap‬
‭ )‬

Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‬
‭ photoFile‬
‭ !!.‬
‭ absolutePath‬
‭ ,‬
‭ Toast‬
‭ .‬
‭ LENGTH_LONG‬
‭ ).show()‬

}‬‭
‭ else‬‭{‬
Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‬‭
‭ "Capture cancelled"‬
,‬‭
‭ Toast‬ .‭
‭L
‬ENGTH_SHORT‬ ).show()‬

}‬

}‬

override fun‬‭
‭ onCreate‬
(‭
‭s‬avedInstanceState‬ :‬‭
‭ Bundle‬
?)‬‭
‭ {‬
super‬
‭ .onCreate(‬
‭ savedInstanceState‬
‭ )‬

setContentView(‬
‭ R‬
‭ .‭
‭l‬ayout‬
.‬
‭ activity_main‬
‭ )‬

‭mageView‬‭
i = findViewById(‬R‭
‭.‬‬
id‬
‭ .‭
‭i‬mageView‬
)‬

btnCapture‬‭
‭ = findViewById(‬
R‬
‭ .‭
‭i‬d‬
.‬
‭ btn‬
‭ )‬

btnCapture‬
‭ .setOnClickListener‬‭
‭ {‬
val‬‭
‭ cameraPermission‬‭ =‬‭
ActivityCompat‬
.checkSelfPermission(‬
‭ this‬
‭ ,‬

Manifest‬
‭ .‬
‭ permission‬
‭ .‭
‭C‬AMERA‬
)‬

val‬‭
‭ storagePermission‬‭ =‬‭
ActivityCompat‬
.checkSelfPermission(‬
‭ this‬
‭ ,‬

Manifest‬
‭ .‬
‭ permission‬
‭ .‭
‭W‬RITE_EXTERNAL_STORAGE‬
)‬

‭f‬
i (‬
‭ cameraPermission‬‭
‭ ==‬‭PackageManager‬ .‭
‭P‬ERMISSION_GRANTED‬‭ &&‬‭
storagePermission‬
==‬‭
‭ PackageManager‬ .‭
‭P‬ERMISSION_GRANTED‬ ){‬

capturePhoto()‬

}‬

else‬‭
‭ {‬
val‬‭
‭ permissions‬‭ =‬‭mutableListOf‬(‭
‭M‬anifest‬ .‭
‭p
‬ermission‬.‭
‭C‬AMERA‬
)‬

if‬‭
‭ (‬
Build‬
‭ .‬
‭ VERSION‬
‭ .‭
‭S‬DK_INT‬‭
<‬‭
Build‬.‭
‭V
‬ERSION_CODES‬ .‭
‭T‬IRAMISU‬)‬‭
‭ {‬
permissions‬
‭ .add(‬
‭ Manifest‬
‭ .‬
‭ permission‬
‭ .‭
‭W‬RITE_EXTERNAL_STORAGE‬
)‬

}‬

requestPermissions(‬
‭ permissions‬
‭ .‭
‭t
‬oTypedArray‬(),‬‭
‭ 101‬
)‬

}‬

}‬

}‬

‭verride fun‬‭
o onRequestPermissionsResult‬ (‬
‭ requestCode‬
‭ :‬‭
‭ Int‬
,‬‭
‭ permissions‬
:‬

Array‬
‭ <‬
‭ out‬‭
‭ String‬>,‬‭
‭ grantResults‬:‬‭
‭ IntArray‬) {‬

super‬
‭ .onRequestPermissionsResult(‬
‭ requestCode‬
‭ ,‬‭
‭ permissions‬,‬

grantResults‬
‭ )‬

if‬‭
‭ (‬
requestCode‬‭
‭ ==‬‭101‬‭
&&‬‭
grantResults‬ .‭
‭i‬sNotEmpty‬ ()‬‭
‭ &&‬
grantResults‬
‭ .‭
‭a‬ll‬‭{ it‬‭
==‬‭PackageManager‬
.‬
‭ PERMISSION_GRANTED‬‭
‭ }‬
) {‬

capturePhoto()‬

}‬

else‬‭
‭ {‬
Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‬‭
‭ "Permissions denied"‬ ,‬‭
‭ Toast‬.‭
‭L‬ENGTH_SHORT‬
).show()‬

}‬

}‬

private fun‬‭
‭ capturePhoto‬ () {‬

val‬‭
‭ intent‬‭= Intent(‬ MediaStore‬
‭ .‬
‭ ACTION_IMAGE_CAPTURE‬
‭ )‬

val‬‭
‭ timeStamp‬ :‬‭
‭ String‬‭
=‬
SimpleDateFormat(‬
‭ "yyyyMMdd_HHmmss"‬
‭ ).format(Date())‬

val‬‭
‭ storageDir‬ :‬‭
‭ File‬
? =‬

getExternalFilesDir(‬
‭ Environment‬
‭ .‬
‭ DIRECTORY_PICTURES‬
‭ )‬

photoFile‬‭
‭ =‬‭File‬ .createTempFile(‬
‭ "JPEG_‬
‭ ${‬
‭ timeStamp‬
‭ }‬
‭ _"‬
‭ ,‬‭
‭ ".jpg"‬
,‬

storageDir‬
‭ )‬

if‬
‭ (‭
‭p‬hotoFile‬‭ !=‬‭null‬
) {‬

photoUri‬‭
‭ =‬‭FileProvider‬ .getUriForFile(‬
‭ this‬
‭ ,‬

"‬
‭ ${‬
‭ applicationContext‬
‭ .‭
‭p‬ackageName‬ }‭
‭.‬provider"‬ ,‬‭
‭ photoFile‬ !!)‬

intent‬
‭ .putExtra(‬
‭ MediaStore‬
‭ .‬
‭ EXTRA_OUTPUT‬
‭ ,‬‭
‭ photoUri‬ )‬

cameraLauncher‬
‭ .launch(‬
‭ intent‬
‭ )‬

}‬

}‬

}‬

‭4. Accessing Bluetooth‬

‭ luetoothAdapter‬‭is‬‭a‬‭class‬‭in‬‭Android‬‭that‬‭represents‬‭the‬‭device's‬‭Bluetooth‬‭adapter,‬‭which‬
B
‭is‬‭the‬‭hardware‬‭component‬‭that‬‭manages‬‭Bluetooth‬‭communication.‬‭This‬‭class‬‭allows‬‭you‬‭to‬
‭perform‬ ‭various‬ ‭Bluetooth‬ ‭operations,‬ ‭such‬ ‭as‬ ‭checking‬ ‭if‬ ‭Bluetooth‬ ‭is‬ ‭supported,‬
‭enabling/disabling Bluetooth, discovering devices, and managing paired devices.‬

‭Key Features of BluetoothAdapter:‬

‭●‬ G‭ et‬ ‭the‬ ‭Default‬ ‭Adapter:‬ ‭You‬ ‭can‬ ‭get‬ ‭the‬ ‭default‬ ‭Bluetooth‬ ‭adapter‬ ‭by‬ ‭calling‬
‭BluetoothAdapter.getDefaultAdapter().‬

‭val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()‬

‭●‬ C‭ heck‬ ‭Bluetooth‬ ‭Support:‬ ‭Before‬ ‭attempting‬ ‭to‬ ‭use‬ ‭Bluetooth‬ ‭features,‬ ‭you‬ ‭can‬
‭check if the device supports Bluetooth.‬

‭if (bluetoothAdapter == null) {‬


‭Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show()‬
‭}‬

‭●‬ E‭ nable‬ ‭Bluetooth:‬ ‭You‬ ‭can‬ ‭enable‬ ‭Bluetooth‬ ‭programmatically‬ ‭(if‬ ‭not‬ ‭already‬
‭enabled) or prompt the user to enable it:‬

‭if (!bluetoothAdapter.isEnabled) {‬
‭val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)‬
‭startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)‬
‭}‬

‭●‬ G‭ et‬ ‭Paired‬ ‭Devices:‬ ‭You‬ ‭can‬ ‭get‬ ‭a‬ ‭set‬ ‭of‬ ‭paired‬ ‭devices‬ ‭(bonded‬ ‭devices)‬ ‭using‬
‭bondedDevices‬‭:‬

‭val pairedDevices: Set<BluetoothDevice> = bluetoothAdapter.bondedDevices‬

‭●‬ D‭ iscover‬‭Devices:‬‭To‬‭discover‬‭nearby‬‭Bluetooth‬‭devices,‬‭you‬‭can‬‭start‬‭the‬‭discovery‬
‭process:‬

‭val discoveryStarted = bluetoothAdapter.startDiscovery()‬

‭●‬ ‭Check Bluetooth State‬‭: You can also check the current‬‭state of Bluetooth using the‬
BluetoothAdapter‬‭state constants:‬

‭ luetoothAdapter.STATE_ON‬
B
‭BluetoothAdapter.STATE_OFF‬
‭BluetoothAdapter.STATE_TURNING_ON‬
‭BluetoothAdapter.STATE_TURNING_OFF‬
‭Example Program to display all Bluetooth Paired Devices‬

‭Step 1: Add Permission in AndroidManifest.xml‬

‭‭
<u‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.BLUETOOTH_CONNECT"‬
‭ />‬

‭Step 2: Design the Interface with Button and ListView‬

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

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

xmlns:‬
‭ app‬
‭ ="http://schemas.android.com/apk/res-auto"‬

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

android‬
‭ :id‬
‭ ="@+id/main"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

tools‬
‭ :context‬
‭ =".MainActivity"‬

android‬
‭ :orientation‬
‭ ="vertical"‬

>‬

<‭
‭B
‬utton‬
android‬
‭ :id‬
‭ ="@+id/button"‬

android‬
‭ :layout_width‬
‭ ="200dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="View Paired Devices"‬

android‬
‭ :layout_gravity‬
‭ ="center_horizontal"‬

android‬
‭ :layout_marginTop‬
‭ ="50dp"‬

/>‬

<‭
‭L
‬istView‬
android‬
‭ :id‬
‭ ="@+id/listView"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

android‬
‭ :layout_marginTop‬
‭ ="20dp"‬

android‬
‭ :padding‬
‭ ="20dp"‬

/>‬

</‬
‭ LinearLayout‬
‭ >‬

‭Step 3: Implement the Kotlin code to display all paired devices.‬

‭mport‬‭
i android.bluetooth.‬
BluetoothAdapter‬

import‬‭
‭ android.bluetooth.‬
BluetoothDevice‬

import‬‭
‭ android.content.pm.‬PackageManager‬

import‬‭
‭ android.os.‬
Bundle‬

import‬‭
‭ android.widget.‬
ArrayAdapter‬

import‬‭
‭ android.widget.‬
Button‬

import‬‭
‭ android.widget.‬
ListView‬

import‬‭
‭ android.widget.‬
TextView‬

import‬‭
‭ android.widget.‬
Toast‬

import‬‭
‭ androidx.activity.enableEdgeToEdge‬
import‬‭
‭ androidx.appcompat.app.‬AppCompatActivity‬

class‬‭
‭ MainActivity‬‭
: AppCompatActivity() {‬
private lateinit var‬‭
‭ tvDevices‬:‬‭
‭ TextView‬
override fun‬‭
‭ onCreate‬(‬
‭ savedInstanceState‬
‭ :‬‭
‭ Bundle‬
?)‬‭
‭ {‬
super‬
‭ .onCreate(‬
‭ savedInstanceState‬
‭ )‬

enableEdgeToEdge‬
‭ ()‬

setContentView(‬
‭ R‭
‭ .
‬‭
l
‬ayout‬.‭
‭a
‬ctivity_main‬
)‬

‭al‬‭
v button‬
:‭
‭B
‬utton‬‭= findViewById(‬
R‬
‭ .‬
‭ id‬
‭ .‬
‭ button‬
‭ )‬

val‬‭
‭ listView‬:‭
‭L
‬istView‬‭
= findViewById(‬ R‬
‭ .‬
‭ id‬
‭ .‬
‭ listView‬
‭ )‬

val‬‭
‭ bluetoothAdapter‬
:‭
‭B‬luetoothAdapter‬‭
=‬
BluetoothAdapter‬
‭ .getDefaultAdapter()‬

‭f‬
i (checkSelfPermission(android.‬
‭ Manifest‬
‭ .‭
‭p
‬ermission‬
.‬
‭ BLUETOOTH_CONNECT‬
‭ )!=‬

PackageManager‬
‭ .‭
‭P
‬ERMISSION_GRANTED‬
) {‬

‭equestPermissions(‬
r arrayOf‬
‭ (android.‬
‭ Manifest‬
‭ .‭
‭p
‬ermission‬
.‭
‭ B
‬LUETOOTH_CONNECT‬
),‬

1‬
‭)‬

}‬

button‬
‭ .setOnClickListener‬‭
‭ {‬
val‬‭
‭ pairedDevices‬ :‬‭
‭ Set‬
<‬
‭ BluetoothDevice‬
‭ >‬‭
‭ =‬
bluetoothAdapter‬
‭ .‭
‭b
‬ondedDevices‬
val‬‭
‭ deviceList‬‭ =‬‭
mutableListOf‬ <‬
‭ String‬
‭ >()‬

if‬‭
‭ (‬pairedDevices‬
‭ .‬
‭ isNotEmpty‬
‭ ()) {‬

for‬‭
‭ (‭
d‬evice‬‭
in‬‭ pairedDevices‬ ) {‬

deviceList‬
‭ .add(‬
‭ "‭
‭$
‬{‬device‬
‭ .‭
‭n
‬ame‬
}‬‭
‭ (‭
$
‬{‬device‬
‭ .‭
‭a
‬ddress‬
}‭
‭)‬"‬
)‬

}‬

val‬‭
‭ adapter‬‭= ArrayAdapter(‬ this‬
‭ ,‬

android.‬
‭ R‭
‭.
‬‬layout‬
‭ .‭
‭s
‬imple_list_item_1‬ ,‬‭
‭ deviceList‬ )‬

listView‬
‭ .‭
‭a
‬dapter‬‭ =‬‭adapter‬
}‬‭
‭ else‬‭ {‬
Toast‬
‭ .makeText(‬
‭ this‬
‭ ,‬‭
‭ "No paired devices‬‭ found"‬,‬

Toast‬
‭ .‬
‭LENGTH_SHORT‬
‭ ).show()‬

}‬

}‬

}‬

}‬

‭5. Telephony API‬

‭ he‬‭Android telephony APIs‬‭let your applications access‬‭the underlying telephone hardware‬


T
‭stack and allows to integrate call handling and phone state monitoring into your applications.‬
‭It provides access to cellular network information, SIM details, call states, SMS management,‬
‭and more. Here's how you can use the Telephony API in Kotlin for common tasks.‬
‭TelephonyManager‬

‭ he‬‭TelephonyManager‬‭class in Android provides access‬‭to information about the cellular‬


T
‭network, SIM cards, and phone state.‬
‭Get an Instance of TelephonyManager‬

‭You can access the‬‭TelephonyManager‬‭by using the‬‭getSystemService()‬‭method:‬

val‬‭
‭ telephonyManager‬‭
= getSystemService(‬Context‬
‭ .‭
‭T‬ELEPHONY_SERVICE‬
)‬

as‬‭
‭ TelephonyManager‬

‭Accessing SIM Card, Phone and Network Information‬

‭Permission Required‬

‭‭
<u‬ses-permission‬
android‬
‭ :name‬
‭ ="android.permission.READ_PHONE_STATE"‬
‭ />‬

‭Accessing IMEI and Subscriber ID‬

val‬‭
‭ telephonyManager‬‭
= getSystemService(‬Context‬
‭ .‭
‭T‬ELEPHONY_SERVICE‬
)‬

as‬‭
‭ TelephonyManager‬
val‬‭
‭ imei‬‭
=‬‭
telephonyManager‬
.‭
‭ i
‬mei‬
val‬‭
‭ subscriberId‬‭
=‬‭
telephonyManager‬
.‭
‭ s
‬ubscriberId‬

‭ ote:‬‭From Android 10 onwards, access to‬‭IMEI‬‭and‬‭Subscriber ID‬‭is restricted unless‬


N
‭your app is set as the‬‭default SMS app, carrier app,‬‭or device owner‬‭.‬

‭Get Network and SIM Details‬

val‬‭
‭ telephonyManager‬‭= getSystemService(‬ Context‬
‭ .‭
‭T
‬ELEPHONY_SERVICE‬
)‬

as‬‭
‭ TelephonyManager‬
val‬‭
‭ simOperatorName‬‭=‬‭telephonyManager‬ .‬
‭ simOperatorName‬

val‬‭
‭ networkOperatorName‬‭ =‬‭
telephonyManager‬ .‬
‭ networkOperatorName‬

val‬‭
‭ countryIso‬‭
=‬‭telephonyManager‬.‬
‭ simCountryIso‬

val‬‭
‭ networkType‬‭=‬‭
when‬‭(‭
t
‬elephonyManager‬ .‭
‭d
‬ataNetworkType‬ )‬‭
‭ {‬
TelephonyManager‬
‭ .‭
‭N
‬ETWORK_TYPE_LTE‬‭ ->‬‭"4G LTE"‬
TelephonyManager‬
‭ .‭
‭N
‬ETWORK_TYPE_NR‬‭ ->‬‭"5G"‬
else‬‭
‭ ->‬‭
"Unknown Network"‬
}‬

‭Make Phone Calls using TelephonyManager‬

‭ e are able to make a phone call in android via implicit intent. You need to set two‬
W
‭permissions in AndroidManifest.xml‬

‭‬
<uses-permission‬‭
‭ android‬
:name‬
‭ ="android.permission.CALL_PHONE"‬
‭ />‬

<‬
‭uses-feature‬‭
‭ android‬
:name‬
‭ ="android.hardware.telephony"‬

android‬
‭ :required‬
‭ ="false"‬‭
‭ />‬
‭ o‬‭make‬‭call‬‭directly‬‭from,‬‭you‬‭also‬‭need‬‭to‬‭have‬‭run‬‭time‬‭permission‬‭in‬‭kotlin‬‭code‬‭as‬
T
‭given below‬

‭if‬‭(‭A
‬ ctivityCompat‬‭.checkSelfPermission(‬‭this‬‭,‭M‬ anifest‬‭.‭p
‬ ermission‬‭.‬‭CALL_PHONE‬‭)!=‬
‭PackageManager‬‭.‭P ‬ ERMISSION_GRANTED‬‭)‬
‭{‬
‭ActivityCompat‬‭.requestPermissions(‬‭this‬‭,‬‭arrayOf‬‭(‭M
‬ anifest‬‭.‭p
‬ ermission‬‭.‭C
‬ ALL_PHONE‬‭),‬
‭PackageManager‬‭.‭P ‬ ERMISSION_GRANTED‬‭)‬
‭}‬

‭Kotlin code to make phone calls directly from your app‬

‭class ‭M‬ ainActivity ‬‭: AppCompatActivity() {‬


‭override fun ‭o
‬ nCreate‬‭(‭s‬ avedInstanceState‬‭: ‬‭Bundle‬‭?)‬‭{‬
‭super‬‭.onCreate(‬‭savedInstanceState‬‭)‬
‭ setContentView(‬‭R‬‭.‭l‬ayout‬‭.‬‭activity_main‬‭)‬

‭ al ‬‭phoneno‬‭:‬‭EditText ‭=
v ‬ findViewById(‬‭R‬‭.‭i‬d‬‭.‭p ‬ hone‬‭)‬
‭val ‬‭btn‬‭: ‬‭Button ‭=
‬ findViewById(‬‭R‬‭.‭i‬d‬‭.‭b
‬ tn‬‭)‬

‭btn‬‭.setOnClickListener ‬‭{‬
‭//Request User Permission‬
‭if‬‭(‬‭ActivityCompat‬‭.checkSelfPermission(‬‭this‬‭,‭M ‬ anifest‬‭.‬‭permission‬‭.‬‭CALL_PHONE‬‭)!=‬
‭PackageManager‬‭.‭P ‬ ERMISSION_GRANTED‬‭)‬
‭ {‬
‭ActivityCompat‬‭.requestPermissions(‬‭this‬‭,‬‭arrayOf‬‭(‭M ‬ anifest‬‭.‬‭permission‬‭.‭C
‬ ALL_PHONE‬‭),‬
‭PackageManager‬‭.‭P ‬ ERMISSION_GRANTED‬‭)‬
‭ }‬
‭else ‬‭{‬
‭var ‬‭intent‬‭: ‭I‬ntent ‬‭= Intent(‬‭Intent‬‭.‭A
‬ CTION_CALL‬‭)‬
‭intent‬‭.setData((‬‭Uri‬‭.parse(‬‭"tel:" ‭+‬ ‬‭phoneno‬‭.‬‭text‬‭.toString())))‬
‭ startActivity(‬‭intent‬‭)‬
‭ }‬

‭}‬
‭}‬
‭}‬

‭Design Code‬

‭?‬
< 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/main"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

tools‬
‭ :context‬
‭ =".MainActivity"‬

android‬
‭ :orientation‬
‭ ="vertical"‬

>‬

<‬
‭EditText‬

android‬
‭ :id‬
‭ ="@+id/phone"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬
‭ />‬

<‬
‭Button‬

android‬
‭ :id‬
‭ ="@+id/btn"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

‭ndroid‬
a :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="Call"‬

/>‬

</‬
‭ LinearLayout‬
‭ >‬

‭6. Working with Sensors‬

‭ ost‬ ‭Android-powered‬ ‭devices‬ ‭have‬ ‭built-in‬ ‭sensors‬ ‭that‬ ‭measure‬ ‭motion,‬ ‭orientation,‬‭and‬
M
‭various environmental conditions.‬

‭ hese‬ ‭sensors‬ ‭are‬ ‭capable‬ ‭of‬ ‭providing‬ ‭raw‬ ‭data‬ ‭with‬ ‭high‬ ‭precision‬‭and‬‭accuracy,‬‭and‬‭are‬
T
‭useful‬‭if‬‭you‬‭want‬‭to‬‭monitor‬‭three-dimensional‬‭device‬‭movement‬‭or‬‭positioning,‬‭or‬‭you‬‭want‬
‭to monitor changes in the ambient environment near a device‬

‭ or‬ ‭example,‬ ‭a‬ ‭game‬ ‭might‬ ‭track‬ ‭readings‬ ‭from‬ ‭a‬ ‭device's‬ ‭gravity‬‭sensor‬‭to‬‭infer‬‭complex‬
F
‭user gestures and motions, such as tilt, shake, rotation, or swing.‬

‭ ikewise,‬ ‭a‬ ‭weather‬ ‭application‬ ‭might‬ ‭use‬ ‭a‬ ‭device's‬ ‭temperature‬ ‭sensor‬ ‭and‬ ‭humidity‬
L
‭sensor‬‭to‬‭calculate‬‭and‬‭report‬‭the‬‭dewpoint,‬‭or‬‭a‬‭travel‬‭application‬‭might‬‭use‬‭the‬‭geomagnetic‬
‭field sensor and accelerometer to report a compass bearing.‬

‭The Android platform supports three broad categories of sensors:‬

‭ otion sensors‬
M
‭These‬ ‭sensors‬ ‭measure‬ ‭acceleration‬ ‭forces‬ ‭and‬ ‭rotational‬ ‭forces‬ ‭along‬ ‭three‬ ‭axes.‬ ‭This‬
‭category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.‬

‭ nvironmental sensors‬
E
‭These‬ ‭sensors‬ ‭measure‬ ‭various‬ ‭environmental‬ ‭parameters,‬ ‭such‬ ‭as‬ ‭ambient‬ ‭air‬ ‭temperature‬
‭and‬ ‭pressure,‬ ‭illumination,‬ ‭and‬ ‭humidity.‬ ‭This‬ ‭category‬ ‭includes‬ ‭barometers,‬ ‭photometers,‬
‭and thermometers.‬

‭ osition sensors‬
P
‭These‬ ‭sensors‬ ‭measure‬ ‭the‬ ‭physical‬ ‭position‬ ‭of‬‭a‬‭device.‬‭This‬‭category‬‭includes‬‭orientation‬
‭sensors and magnetometers.‬

‭Sensor Types supported by the Android platform.‬

‭Table 1.‬‭‬‭Sensor Types‬‭supported by the Android platform.‬

‭ ommon‬
C
‭Sensor‬ ‭Type‬ ‭Description‬
‭Uses‬
‭TYPE_ACCELEROMETER‬ ‭ easures the acceleration force in m/s‬‭2‬‭that‬
‭Hardware‬ M ‭ otion‬
M
‭is applied to a device on all three physical‬ ‭detection‬
‭axes (x, y, and z), including the force of‬ ‭(shake, tilt,‬
‭gravity.‬ ‭etc.).‬
‭TYPE_AMBIENT_TEMPERATURE‬ ‭Hardware‬ ‭Measures the ambient room temperature in‬ ‭Monitoring‬
‭degrees Celsius (°C). See note below.‬ ‭air‬
‭temperatures.‬
‭TYPE_GRAVITY‬ ‭ oftware or‬ ‭Measures the force of gravity in m/s‬‭2‬‭that is‬ ‭Motion‬
S
‭Hardware‬ ‭applied to a device on all three physical axes‬‭detection‬
‭(x, y, z).‬ ‭(shake, tilt,‬
‭etc.).‬
‭TYPE_GYROSCOPE‬ ‭Hardware‬ ‭Measures a device's rate of rotation in rad/s‬ ‭Rotation‬
‭around each of the three physical axes (x, y,‬ ‭detection‬
‭and z).‬ ‭(spin, turn,‬
‭etc.).‬
‭TYPE_LIGHT‬ ‭Hardware‬ ‭Measures the ambient light level‬ ‭Controlling‬
‭(illumination) in lx.‬ ‭screen‬
‭brightness.‬
‭2‬
‭TYPE_LINEAR_ACCELERATION‬ ‭Software or‬ ‭Measures the acceleration force in m/s‬ ‭that‬ ‭Monitoring‬
‭Hardware‬ ‭is applied to a device on all three physical‬ ‭acceleration‬
‭axes (x, y, and z), excluding the force of‬ ‭along a single‬
‭gravity.‬ ‭axis.‬
‭ YPE_MAGNETIC_FIELD‬
T ‭Hardware‬ ‭Measures the ambient geomagnetic field for‬ ‭Creating a‬
‭all three physical axes (x, y, z) in μT.‬ ‭compass.‬
‭TYPE_ORIENTATION‬ ‭Software‬ ‭Measures degrees of rotation that a device‬ ‭Determining‬
‭makes around all three physical axes (x, y,‬ ‭device‬
‭z). As of API level 3 you can obtain the‬ ‭position.‬
‭inclination matrix and rotation matrix for a‬
‭device by using the gravity sensor and the‬
‭geomagnetic field sensor in conjunction‬
‭with the ‬‭getRotationMatrix()‬‭method.‬
‭TYPE_PRESSURE‬ ‭Hardware‬ ‭Measures the ambient air pressure in hPa or‬ ‭Monitoring‬
‭mbar.‬ ‭air pressure‬
‭changes.‬
‭TYPE_PROXIMITY‬ ‭Hardware‬ ‭Measures the proximity of an object in cm‬ ‭Phone‬
‭relative to the view screen of a device. This‬ ‭position‬
‭sensor is typically used to determine‬ ‭during a call.‬
‭whether a handset is being held up to a‬
‭person's ear.‬
‭TYPE_RELATIVE_HUMIDITY‬ ‭Hardware‬ ‭Measures the relative ambient humidity in‬ ‭Monitoring‬
‭percent (%).‬ ‭dewpoint,‬
‭absolute, and‬
‭relative‬
‭humidity.‬
‭TYPE_ROTATION_VECTOR‬ ‭Software or‬ ‭Measures the orientation of a device by‬ ‭Motion‬
‭Hardware‬ ‭providing the three elements of the device's‬ ‭detection and‬
‭rotation vector.‬ ‭rotation‬
‭detection.‬
‭TYPE_TEMPERATURE‬ ‭Hardware‬ ‭Measures the temperature of the device in‬
‭degrees Celsius (°C). This sensor‬
‭implementation varies across devices and‬
‭this sensor was replaced with‬
‭the ‭T
‬ YPE_AMBIENT_TEMPERATURE‬‭s‬
‭ensor in API Level 14‬

‭Sensor Framework‬

‭SensorManager‬

‭ ou‬‭can‬‭use‬‭this‬‭class‬‭to‬‭create‬‭an‬‭instance‬‭of‬‭the‬‭sensor‬‭service.‬‭This‬‭class‬‭provides‬
Y
‭various‬‭methods‬‭for‬‭accessing‬‭and‬‭listing‬‭sensors,‬‭registering‬‭and‬‭unregistering‬‭sensor‬
‭event‬‭listeners,‬‭and‬‭acquiring‬‭orientation‬‭information.‬‭This‬‭class‬‭also‬‭provides‬‭several‬
s‭ ensor‬‭constants‬‭that‬‭are‬‭used‬‭to‬‭report‬‭sensor‬‭accuracy,‬‭set‬‭data‬‭acquisition‬‭rates,‬‭and‬
‭calibrate sensors.‬

‭Sensor‬

‭ ou can use this class to create an instance of a specific sensor. This class provides‬
Y
‭various methods that let you determine a sensor's capabilities.‬

‭SensorEvent‬

‭ he‬‭system‬‭uses‬‭this‬‭class‬‭to‬‭create‬‭a‬‭sensor‬‭event‬‭object,‬‭which‬‭provides‬‭information‬
T
‭about‬ ‭a‬ ‭sensor‬ ‭event.‬ ‭A‬ ‭sensor‬ ‭event‬ ‭object‬ ‭includes‬ ‭the‬ ‭following‬‭information:‬‭the‬
‭raw‬‭sensor‬‭data,‬‭the‬‭type‬‭of‬‭sensor‬‭that‬‭generated‬‭the‬‭event,‬‭the‬‭accuracy‬‭of‬‭the‬‭data,‬
‭and the timestamp for the event.‬

‭SensorEventListener‬

‭ ou can use this interface to create two callback methods that receive notifications‬
Y
‭(sensor events) when sensor values change or when sensor accuracy changes.‬

‭Creating SensorManager object‬

‭al‬‭
v sensorManager‬‭
= getSystemService(‬
Context‬
‭ .‭
‭S‬ENSOR_SERVICE‬
)‬‭
‭ as‬
SensorManager‬

‭Design the UI‬

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

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

xmlns:‬
‭ app‬
‭ ="http://schemas.android.com/apk/res-auto"‬

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

android‬
‭ :id‬
‭ ="@+id/main"‬

android‬
‭ :layout_width‬
‭ ="match_parent"‬

android‬
‭ :layout_height‬
‭ ="match_parent"‬

tools‬
‭ :context‬
‭ =".MainActivity"‬

android‬
‭ :orientation‬
‭ ="vertical"‬
‭ >‬

<‭
‭B
‬utton‬
android‬
‭ :id‬
‭ ="@+id/viewBtn"‬

android‬
‭ :layout_width‬
‭ ="wrap_content"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :text‬
‭ ="View All Sensors"‬

android‬
‭ :layout_marginTop‬
‭ ="40dp"‬

android‬
‭ :layout_gravity‬
‭ ="center_horizontal"‬

/>‬

<‭
‭L
‬istView‬
android‬
‭ :id‬
‭ ="@+id/listView"‬

android‬
‭ :layout_width‬
‭ ="379dp"‬

android‬
‭ :layout_height‬
‭ ="wrap_content"‬

android‬
‭ :layout_gravity‬
‭ ="center_horizontal"‬

android‬
‭ :layout_marginTop‬
‭ ="20dp"‬

android‬
‭ :textColor‬
‭ ="#D81B60"‬

‭ndroid‬
a :textSize‬
‭ ="25dp"‬

/>‬

</‬
‭ LinearLayout‬
‭ >‬

‭mport‬‭
i android.content.‬Context‬

import‬‭
‭ android.hardware.‬Sensor‬

import‬‭
‭ android.hardware.‬SensorManager‬

import‬‭
‭ android.os.‬
Bundle‬

import‬‭
‭ android.widget.‬
ArrayAdapter‬

import‬‭
‭ android.widget.‬
Button‬

import‬‭
‭ android.widget.‬
ListView‬

import‬‭
‭ androidx.activity.enableEdgeToEdge‬
import‬‭
‭ androidx.appcompat.app.‬AppCompatActivity‬

class‬‭
‭ MainActivity‬‭ : AppCompatActivity() {‬
override fun‬‭
‭ onCreate‬(‬
‭ savedInstanceState‬
‭ :‬‭
‭ Bundle‬ ?)‬‭
‭ {‬
super‬
‭ .onCreate(‬
‭ savedInstanceState‬
‭ )‬

enableEdgeToEdge‬
‭ ()‬

setContentView(‬
‭ R‭
‭.
‬‭
l
‬ayout‬ .‭
‭a
‬ctivity_main‬ )‬

val‬‭
‭ btn‬
:‬
‭ Button‬‭
‭ = findViewById(‬ R‬
‭ .‭
‭i
‬d‬.‬
‭ viewBtn‬
‭ )‬

val‬‭
‭ listView‬:‬‭
‭ ListView‬‭ = findViewById(‬ R‬
‭ .‭
‭i
‬d‬.‬
‭ listView‬
‭ )‬

val‬‭
‭ sensorManager‬‭ = getSystemService(‬ Context‬
‭ .‭
‭S
‬ENSOR_SERVICE‬
)‬‭
‭ as‬
SensorManager‬

btn‬
‭ .setOnClickListener‬‭
‭ {‬
// Initialize the SensorManager‬

val‬‭
‭ sensorManager‬‭ = getSystemService(‬ Context‬
‭ .‭
‭S
‬ENSOR_SERVICE‬
)‬

as‬‭
‭ SensorManager‬
// Get a list of all available sensors‬

val‬‭
‭ sensorList‬ :‬‭
‭ List‬<‭
‭S
‬ensor‬ > =‬

sensorManager‬
‭ .getSensorList(‬
‭ Sensor‬
‭ .‬
‭ TYPE_ALL‬
‭ )‬

// Extract sensor names into a string‬‭
‭ list‬
val‬‭
‭ sensorNames‬‭ =‬‭
sensorList‬ .‭
‭m
‬ap‬‭ { it‬ .‬
‭ name‬‭
‭ }‬
// Set up an ArrayAdapter to display sensor‬‭
‭ names in the‬
ListView‬

val‬‭
‭ adapter‬‭= ArrayAdapter(‬ this‬
‭ ,‬

android.‬
‭ R‭
‭.
‬‬layout‬
‭ .‭
‭s
‬imple_list_item_1‬ ,‬‭
‭ sensorNames‬ )‬

listView‬
‭ .‭
‭a
‬dapter‬‭ =‬‭adapter‬
}‬

}‬

}‬

You might also like