SlideShare a Scribd company logo
1 of 22
Android App Development
Lesson 10 - Styles, Permissions & BroadcastReceiver
This Leson
● Styling your app
● App Permission Basics
● Creating & Registering a BroadcastReceiver.
Styling Your App
● Similiar to styling webpages in a separate CSS file,
Android Apps can be given their own style in a separate
XML file.
● This is another new XML file within resources and
resides in the res/values directory. Can be called
anything (e.g. Often style.xml)
● Typical properties which are styled are Font, Colour,
Padding, Height, Border and many more.
Styling Parameters
● We can put the styles of GUI components (e.g. TextView,
Button etc) with them in their respective GUI XML files.
However it is cleaner and easier to manage style related
parameters in a separate XML file.
● We can define as many styles as we wish in our styles XML
file using separately named <style></style> tags within the
overall <resource></resource> tags.
● Between each set of <style></style> tags we put one or more
<item></item> tags for each property that style wishes to use.
A Basic Style XML File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="style1">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">30sp</item>
</style>
<style name="style2">
<item
name="android:background">#C0C0C0</item>
</style>
sp = Scale
Independent
Pixel. Should
be used for
sizing fonts.
dp for
everything
else.
Using a <style>
● Every <style> has a name. We use this to refer to it in
our GUI XML to style a component (or components)
● If we had a TextView in an Activity or Fragment which
we wished to style using style1 from previous slide.
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/welcome_message”
style=”@style/style1”
/>
layout_width
and
layout_height
often placed
in style XML
file.
Themes
A Theme is just a <style> but applied to a whole Activity
or Application. Done in AndroidManifest.xml.
To apply a Theme to an <activity>
<activity android:theme="@android:style/style1">
To apply a Theme to an <application>
<application android:theme="@style/style2">
Style Inheritance
● Somewhat similar to Java class inheritance, one <style>
can extend another. Android platform provides simple
notation to achieve this.
● Inheritance avoids code repetition. Let’s build on top of
textstyle (already defined) and define a new style called
specialtextstyle.
<style name="textstyle.specialtextstyle ">
<item name="android:typeface">monospace</item>
</style>
specialtextstyle
extends from
textstyle and adds
attribute
android:typeface
Tailoring System Themes
● Android comes with a number of pre-existing themes
built-in to the Android platform.
● Often it is easier to just tailor these by changing just
the attribute you need to to suit you apps styling needs.
To extend a system theme you use the parent attribute.
<resources>
<style name="mySpecialTheme" parent="android:Theme.Material">
<item name="android:colorPrimary">put your own value here</item>
<item name="android:colorPrimaryDark">put your own value here</item>
</style>
</resources>
A Useful Diagram
This shows some commonly
styled areas in an app which
are useful to know.
Common Style Attributes
● FAQ : How do I know what attributes I can style ?
● There are a huge number of attributes available to
style in Android (Alot which you’ll probably never use)
● Navigation of this whole area is still not great but start
here in styles.xml (Contains all system default styling)
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml
● Search by component and see what can be styled on it.
(e.g. What can be styled on a Button ?)
Style Example
<style name="Widget.Button">
<item name="background">@drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">@color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>● What we see here is the default styling for a Button. You
can see highlighted in yellow what can be styled.
Android Security
● We’ve seen how each app has its own area to run in
with it’s own internal storage, so it cannot adversley
affect other apps, data or the user’s experience.
● If we write an app which requires resources or data
elsewhere on the device it requests permission.
● Permissions to other data (e.g. Location, Phonebook etc)
must be granted (or rejected) by the user when the
app is being installed. Not possible at runtime.
Asking for Permission
● An app requests a specific permission via the
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.INTERNET"/>
...
</manifest>
● The extensive list of system permissions that are
available to request are here
http://developer.android.com/reference/android/Manifest.permission.html
BroadcastReceiever
Note : These are LOW LEVEL
events to do with the actual
hardware. We’ll see later that we
can listen to software events on
the phone as well. e.g. Phone call
received, SMS received etc
● A BroadcastReceiver is a component which can register
with the Android OS to “listen” to certain system or
application events (NB : These events are Broadcast as Intents)
● Typical SYSTEM BROADCAST would be the following :
o Device Boot Completed
o Power Connected
o Power Disconnected
o Battery Low
o Battery OK
(Constants in Intent class)
Creating a BroadcastReceiever
● Setting up a BroadcastReceiver involves two steps
o Writing the new BroadcastReceiver class
(by extending BroadcastReceiver)
o Registering the new BroadcastReceiver FOR SPECIFIC Intents
(in AndroidManifest.xml)
public class MyBatteryReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
//Do something here
}
}
Events are
represented
as Intents.
Registering a
BroadcastReceiver
● When registering a BroadcastReceiver in the
AndroidManifest.xml we need to define what it is
interested in hearing!
● Within the manifest we must remember two important
things :
o 1. Register for the event (Intent) we’re interested in.
o 2. Have PERMISSION to listen for it.
● NOTE : Most broadcast Intents require that permission
to be granted to an app wishing to hear them.
The Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
<uses-permission android:name="android.permission.BATTERY_STATS" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
</activity>
<receiver android:name=".MyBatteryReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"
/>
</intent-filter>
</receiver>
</application>
</manifest>
Remember : To
mark itself as
being able to
respond to a
particular
Intent, we must
use <intent-
filter>
TelephonyService
● We’ve already seen how to listen to basic system
events to do with the hardware on the phone. Can we
listen for when the phone rings via a BroadcastReceiver
? YES!
● Another constantly running System Service (e.g.
NotifcationService from last week) on your phone is the
TelephonyService.
● TelephonyService has public static final String
constants for possible phone states (EXTRA_STATE_IDLE,
EXTRA_STATE_RINGING, EXTRA_STATE_OFFHOOK)
Responding to Phone State
Changes
● We first need permission to listen to changes in the
state of the phone by adding the following line to
AndroidManifest.xml
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
● Next we need to write our own class (we’ll call it
MyPhoneReceiver) which extends BroadcastReceiver.
● REMEMBER : We receive notifications into our
BroadcastReceiver subclass as an Intent.
(An Intent has a Bundle attached to it called “extras”)
BrodcastReceiver Code
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
}
}
}
extras Bundle
carries
information
about the
phone call
Declare <receiver>
● Remember to declare your BroadcastReceiver in the
AndroidManifest.xml
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"
</action>
</intent-filter>
</receiver>

More Related Content

What's hot

Android Application that makes use of RSS Feed.pptx
Android Application that makes use of RSS Feed.pptxAndroid Application that makes use of RSS Feed.pptx
Android Application that makes use of RSS Feed.pptxvishal choudhary
 
Android application-component
Android application-componentAndroid application-component
Android application-componentLy Haza
 
Android appwidget
Android appwidgetAndroid appwidget
Android appwidgetKrazy Koder
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfacesC.o. Nieto
 
Android Life Cycle
Android Life CycleAndroid Life Cycle
Android Life Cyclemssaman
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barKalluri Vinay Reddy
 
Chapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barChapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barKalluri Vinay Reddy
 
Android app development lesson 1
Android app development lesson 1Android app development lesson 1
Android app development lesson 1Kalluri Vinay Reddy
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layoutKrazy Koder
 
Android Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection WidgetAndroid Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection WidgetPrajyot Mainkar
 
Android development orientation for starters v4 seminar
Android development orientation for starters v4   seminarAndroid development orientation for starters v4   seminar
Android development orientation for starters v4 seminarJoemarie Amparo
 
Android apps development
Android apps developmentAndroid apps development
Android apps developmentRaman Pandey
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
View groups containers
View groups containersView groups containers
View groups containersMani Selvaraj
 

What's hot (20)

Unit2
Unit2Unit2
Unit2
 
Android Application that makes use of RSS Feed.pptx
Android Application that makes use of RSS Feed.pptxAndroid Application that makes use of RSS Feed.pptx
Android Application that makes use of RSS Feed.pptx
 
Android application-component
Android application-componentAndroid application-component
Android application-component
 
Android App Development (Basics)
Android App Development (Basics)Android App Development (Basics)
Android App Development (Basics)
 
Android appwidget
Android appwidgetAndroid appwidget
Android appwidget
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
Android Life Cycle
Android Life CycleAndroid Life Cycle
Android Life Cycle
 
Android development session 3 - layout
Android development   session 3 - layoutAndroid development   session 3 - layout
Android development session 3 - layout
 
Ch2 first app
Ch2 first appCh2 first app
Ch2 first app
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action bar
 
Chapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barChapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action bar
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android app development lesson 1
Android app development lesson 1Android app development lesson 1
Android app development lesson 1
 
android layouts
android layoutsandroid layouts
android layouts
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layout
 
Android Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection WidgetAndroid Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection Widget
 
Android development orientation for starters v4 seminar
Android development orientation for starters v4   seminarAndroid development orientation for starters v4   seminar
Android development orientation for starters v4 seminar
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
View groups containers
View groups containersView groups containers
View groups containers
 

Viewers also liked (8)

A small trip to bluff
A small trip to bluffA small trip to bluff
A small trip to bluff
 
Resume
ResumeResume
Resume
 
Ict 3 mahiti (2)
Ict 3 mahiti (2)Ict 3 mahiti (2)
Ict 3 mahiti (2)
 
Mandya District profile
Mandya District profileMandya District profile
Mandya District profile
 
Case history
Case history Case history
Case history
 
Medical Informatics
Medical InformaticsMedical Informatics
Medical Informatics
 
Bagalkote District profile
Bagalkote District profileBagalkote District profile
Bagalkote District profile
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to Lesson 10

Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in androidMahmudul Hasan
 
Part 2 android application development 101
Part 2 android application development 101Part 2 android application development 101
Part 2 android application development 101Michael Angelo Rivera
 
Android application structure
Android application structureAndroid application structure
Android application structureAlexey Ustenko
 
Hello android world
Hello android worldHello android world
Hello android worldeleksdev
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_programEyad Almasri
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-TejamFandat
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycleKumar
 
Training Session 2 - Day 2
Training Session 2 - Day 2Training Session 2 - Day 2
Training Session 2 - Day 2Vivek Bhusal
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKnoldus Inc.
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android AppsGil Irizarry
 
Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)Kavya Barnadhya Hazarika
 
Android Development project
Android Development projectAndroid Development project
Android Development projectMinhaj Kazi
 
Getting started with android programming
Getting started with android programmingGetting started with android programming
Getting started with android programmingPERKYTORIALS
 
Android Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidAndroid Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidDenis Minja
 

Similar to Lesson 10 (20)

Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in android
 
Android Development Basics
Android Development BasicsAndroid Development Basics
Android Development Basics
 
Part 2 android application development 101
Part 2 android application development 101Part 2 android application development 101
Part 2 android application development 101
 
Android application structure
Android application structureAndroid application structure
Android application structure
 
Migrating JavaME Apps to Android
Migrating JavaME Apps to AndroidMigrating JavaME Apps to Android
Migrating JavaME Apps to Android
 
Hello android world
Hello android worldHello android world
Hello android world
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_program
 
Android session 2
Android session 2Android session 2
Android session 2
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-
 
Android resources in android-chapter9
Android resources in android-chapter9Android resources in android-chapter9
Android resources in android-chapter9
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycle
 
Training Session 2 - Day 2
Training Session 2 - Day 2Training Session 2 - Day 2
Training Session 2 - Day 2
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development Presentation
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android Apps
 
Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)
 
Android Development project
Android Development projectAndroid Development project
Android Development project
 
Getting started with android programming
Getting started with android programmingGetting started with android programming
Getting started with android programming
 
Google Android
Google AndroidGoogle Android
Google Android
 
Android Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidAndroid Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_android
 

Lesson 10

  • 1. Android App Development Lesson 10 - Styles, Permissions & BroadcastReceiver
  • 2. This Leson ● Styling your app ● App Permission Basics ● Creating & Registering a BroadcastReceiver.
  • 3. Styling Your App ● Similiar to styling webpages in a separate CSS file, Android Apps can be given their own style in a separate XML file. ● This is another new XML file within resources and resides in the res/values directory. Can be called anything (e.g. Often style.xml) ● Typical properties which are styled are Font, Colour, Padding, Height, Border and many more.
  • 4. Styling Parameters ● We can put the styles of GUI components (e.g. TextView, Button etc) with them in their respective GUI XML files. However it is cleaner and easier to manage style related parameters in a separate XML file. ● We can define as many styles as we wish in our styles XML file using separately named <style></style> tags within the overall <resource></resource> tags. ● Between each set of <style></style> tags we put one or more <item></item> tags for each property that style wishes to use.
  • 5. A Basic Style XML File <?xml version="1.0" encoding="utf-8"?> <resources> <style name="style1"> <item name="android:textColor">#FF0000</item> <item name="android:textSize">30sp</item> </style> <style name="style2"> <item name="android:background">#C0C0C0</item> </style> sp = Scale Independent Pixel. Should be used for sizing fonts. dp for everything else.
  • 6. Using a <style> ● Every <style> has a name. We use this to refer to it in our GUI XML to style a component (or components) ● If we had a TextView in an Activity or Fragment which we wished to style using style1 from previous slide. <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/welcome_message” style=”@style/style1” /> layout_width and layout_height often placed in style XML file.
  • 7. Themes A Theme is just a <style> but applied to a whole Activity or Application. Done in AndroidManifest.xml. To apply a Theme to an <activity> <activity android:theme="@android:style/style1"> To apply a Theme to an <application> <application android:theme="@style/style2">
  • 8. Style Inheritance ● Somewhat similar to Java class inheritance, one <style> can extend another. Android platform provides simple notation to achieve this. ● Inheritance avoids code repetition. Let’s build on top of textstyle (already defined) and define a new style called specialtextstyle. <style name="textstyle.specialtextstyle "> <item name="android:typeface">monospace</item> </style> specialtextstyle extends from textstyle and adds attribute android:typeface
  • 9. Tailoring System Themes ● Android comes with a number of pre-existing themes built-in to the Android platform. ● Often it is easier to just tailor these by changing just the attribute you need to to suit you apps styling needs. To extend a system theme you use the parent attribute. <resources> <style name="mySpecialTheme" parent="android:Theme.Material"> <item name="android:colorPrimary">put your own value here</item> <item name="android:colorPrimaryDark">put your own value here</item> </style> </resources>
  • 10. A Useful Diagram This shows some commonly styled areas in an app which are useful to know.
  • 11. Common Style Attributes ● FAQ : How do I know what attributes I can style ? ● There are a huge number of attributes available to style in Android (Alot which you’ll probably never use) ● Navigation of this whole area is still not great but start here in styles.xml (Contains all system default styling) https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml ● Search by component and see what can be styled on it. (e.g. What can be styled on a Button ?)
  • 12. Style Example <style name="Widget.Button"> <item name="background">@drawable/btn_default</item> <item name="focusable">true</item> <item name="clickable">true</item> <item name="textAppearance">?attr/textAppearanceSmallInverse</item> <item name="textColor">@color/primary_text_light</item> <item name="gravity">center_vertical|center_horizontal</item> </style>● What we see here is the default styling for a Button. You can see highlighted in yellow what can be styled.
  • 13. Android Security ● We’ve seen how each app has its own area to run in with it’s own internal storage, so it cannot adversley affect other apps, data or the user’s experience. ● If we write an app which requires resources or data elsewhere on the device it requests permission. ● Permissions to other data (e.g. Location, Phonebook etc) must be granted (or rejected) by the user when the app is being installed. Not possible at runtime.
  • 14. Asking for Permission ● An app requests a specific permission via the AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp" > <uses-permission android:name="android.permission.INTERNET"/> ... </manifest> ● The extensive list of system permissions that are available to request are here http://developer.android.com/reference/android/Manifest.permission.html
  • 15. BroadcastReceiever Note : These are LOW LEVEL events to do with the actual hardware. We’ll see later that we can listen to software events on the phone as well. e.g. Phone call received, SMS received etc ● A BroadcastReceiver is a component which can register with the Android OS to “listen” to certain system or application events (NB : These events are Broadcast as Intents) ● Typical SYSTEM BROADCAST would be the following : o Device Boot Completed o Power Connected o Power Disconnected o Battery Low o Battery OK (Constants in Intent class)
  • 16. Creating a BroadcastReceiever ● Setting up a BroadcastReceiver involves two steps o Writing the new BroadcastReceiver class (by extending BroadcastReceiver) o Registering the new BroadcastReceiver FOR SPECIFIC Intents (in AndroidManifest.xml) public class MyBatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Do something here } } Events are represented as Intents.
  • 17. Registering a BroadcastReceiver ● When registering a BroadcastReceiver in the AndroidManifest.xml we need to define what it is interested in hearing! ● Within the manifest we must remember two important things : o 1. Register for the event (Intent) we’re interested in. o 2. Have PERMISSION to listen for it. ● NOTE : Most broadcast Intents require that permission to be granted to an app wishing to hear them.
  • 18. The Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" <uses-permission android:name="android.permission.BATTERY_STATS" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity </activity> <receiver android:name=".MyBatteryReceiver" > <intent-filter> <action android:name="android.intent.action.ACTION_BATTERY_LOW" /> </intent-filter> </receiver> </application> </manifest> Remember : To mark itself as being able to respond to a particular Intent, we must use <intent- filter>
  • 19. TelephonyService ● We’ve already seen how to listen to basic system events to do with the hardware on the phone. Can we listen for when the phone rings via a BroadcastReceiver ? YES! ● Another constantly running System Service (e.g. NotifcationService from last week) on your phone is the TelephonyService. ● TelephonyService has public static final String constants for possible phone states (EXTRA_STATE_IDLE, EXTRA_STATE_RINGING, EXTRA_STATE_OFFHOOK)
  • 20. Responding to Phone State Changes ● We first need permission to listen to changes in the state of the phone by adding the following line to AndroidManifest.xml <uses-permission android:name="android.permission.READ_PHONE_STATE"/> ● Next we need to write our own class (we’ll call it MyPhoneReceiver) which extends BroadcastReceiver. ● REMEMBER : We receive notifications into our BroadcastReceiver subclass as an Intent. (An Intent has a Bundle attached to it called “extras”)
  • 21. BrodcastReceiver Code public class MyPhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { String state = extras.getString(TelephonyManager.EXTRA_STATE); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { String phoneNumber = extras .getString(TelephonyManager.EXTRA_INCOMING_NUMBER); } } } } extras Bundle carries information about the phone call
  • 22. Declare <receiver> ● Remember to declare your BroadcastReceiver in the AndroidManifest.xml <receiver android:name="MyPhoneReceiver" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" </action> </intent-filter> </receiver>