Professional Documents
Culture Documents
Unit 3 (Questions)
Unit 3 (Questions)
Unit No-:03
Android Services and Broadcast Receivers
1) Explain in brief Broadcast and Broadcast Receivers in Android.
Broadcast in android is the system-wide events that can occur when the device starts, when a
message is received on the device or when incoming calls are received, or when a device goes
to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
Broadcast Receivers allow us to register for the system and application events, and when that
event happens, then the register receivers get notified.
Broadcast Receivers simply respond to broadcast messages from other applications or from the
system itself. These messages are sometime called events or intents. For example, applications
can also initiate broadcasts to let other applications know that some data has been downloaded
to the device and is available for them to use, so this is broadcast receiver who will intercept
this communication and will initiate appropriate action.
Since from API Level 26, most of the broadcast can only be caught by the dynamic receiver, so
we have implemented dynamic receivers in our sample project given below. There are some
static fields defined in the Intent class which can be used to broadcast different events. We have
taken a change of airplane mode as a broadcast event, but there are many events for which
broadcast register can be used. Following are some of the important system-wide generated
intents:-
2) How to implement Broadcast receiver in Android.
There are following two important steps to make BroadcastReceiver works for the system
broadcasted intents −
Creating the Broadcast Receiver.
Registering Broadcast Receiver
There is one additional steps in case you are going to implement your custom intents then you
will have to create and broadcast those intents.
Creating the Broadcast Receiver
Broadcast-Receiver
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
Now whenever your Android device gets booted, it will be intercepted by
BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.
There are several system generated events defined as final static fields in the Intent class. The
following table lists a few important system events.
android.intent.action.BATTERY_CHANGED
1 Sticky broadcast containing the charging state, level, and other information about the
battery.
android.intent.action.BATTERY_LOW
2
Indicates low battery condition on the device.
android.intent.action.BATTERY_OKAY
3
Indicates the battery is now okay after being low.
android.intent.action.BOOT_COMPLETED
4
This is broadcast once, after the system has finished booting.
android.intent.action.BUG_REPORT
5
Show activity for reporting a bug.
android.intent.action.CALL
6
Perform a call to someone specified by the data.
android.intent.action.CALL_BUTTON
7 The user pressed the "call" button to go to the dialer or other appropriate UI for placing
a call.
android.intent.action.DATE_CHANGED
8
The date has changed.
android.intent.action.REBOOT
9
Have the device reboot.
3) Write a short note on –
a) Intents in Android
In Android, it is quite usual for users to witness a jump from one application to another as a part
of the whole process, for example, searching for a location on the browser and witnessing a
direct jump into Google Maps or receiving payment links in Messages Application (SMS) and
on clicking jumping to PayPal or GPay (Google Pay). This process of taking users from one
application to another is achieved by passing the Intent to the system. Intents, in general, are
used for navigating among various activities within the same application, but note, is not limited
to one single application, i.e., they can be utilized from moving from one application to another
as well.
Intents could be Implicit, for instance, calling intended actions, and explicit as well, such as
opening another activity after some operations like onClick or anything else. Below are some
applications of Intents:
The collaborative nature of Android applications only results in a better user experience. The
question here is if the intent is for an application that is not present in the device, what’s the
next call?
Some Important Method of Intent and their Description
Methods Description
Deep Link is an URL that redirects the device to the API of that Missing Application and then
the service is run on the system to check if a version of that application exists on the device.
For time being, let’s assume that the application is not available on the device and no previous
versions ever existed. This service then makes a call to the Play Store from the device and the
application appears, just a matter of download.
Not deviating from the topic, there are a few examples that already exist in Android Studio for
redirecting to other applications, for example, Dialing Numbers, Sending SMSs, Opening
Settings, etc. Everyday examples include redirecting to YouTube, Maps, WhatsApp,
Facebook, etc. Android community, especially the Kotlin Community is at its zenith every
single impending day. Kotlin has witnessed a huge amount of sudden growth in the past few
years and could be one of the vital tools in the future replacing Java & possibly Julia also.
Types of Android Intents
There are two types of intents in android
1. Implicit
2. Explicit
Implicit Intent
Implicit Intent doesn’t specify the component. In such a case, intent provides information on
available components provided by the system that is to be invoked. For example, you may
write the following code to view the webpage.
Syntax:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.geeksforgeeks.org/"));
startActivity(intent);
For Example: In the below images, no component is specified, instead, an action is
performed i.e. a webpage is going to be opened. As you type the name of your desired
webpage and click on the ‘CLICK’ button. Your webpage is opened.
Explicit Intent
Explicit Intent specifies the component. In such a case, intent provides the external class to be
invoked.
Syntax:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
For Example: In the below example, there are two activities (FirstActivity, and
SecondActivity). When you click on the ‘GO TO OTHER ACTIVITY’ Button in the
FirstActivity, then you move to the SecondActivity. When you click on the ‘GO TO HOME
ACTIVITY’ button in the SecondActivity, then you move to the first activity. This is getting
done through Explicit Intent.
Note: To know more about the types of intent with example code please refer to Implicit and
Explicit Intents with Examples.
c) System broadcast
The Android system automatically sends broadcasts when various system events occur, such as
when the system switches in and out of airplane mode. The system sends these broadcasts to all
apps that are subscribed to receive the event.
The table below lists the standard system broadcast intents that your app can receive in Android
11 (API level 30). To learn more about each broadcast Intent, use the links below. To learn
more about broadcasts or how to receive them, see the documentation.