You are on page 1of 16

Software for Mobile Devices

Android App Components


Lecture # 12

Online Group Piazza: https://piazza.com/fast_lahore/fall2018/cs440/home


Access Code: cs440
Android App Components

1. Intents
2. Alarm Managers & Job Schedulers
3. Content Providers
4. Notification Manager (Local Notifications & Push
Notifications)
5. Activity vs Fragments
6. Service
7. Broadcast Receiver
8. Contexts
Intents
• An intent is an abstract description of an operation (action) to be
performed.

• It is a way of ‘Message passing between two or more than two


components of android.

• Different activities interact with each other with the help of intent in
Android.

Types & Where can we use them?


1. Using the Intent class you can start activities within your application – Explicit
Intent

2. You can communicate with components defined by the Android system (a.k.a.
Camera, Services, Contacts, browsers, etc.). - Implicit Intent

3. You can perform an action in future via notifications or after some period of
time. – Pending Intent

4. Intent is also used to pass messages between different activities and services.
Intent Filters?

• An intent filter specifies the types of intents to which an activity, service, or


broadcast receiver can respond to by declaring the capabilities of a
component.

• Android components register intent filters either statically in the


AndroidManifest.xml or in case of a broadcast receiver also dynamically
via code.

• Android OS uses filters to pinpoint the set of Activities, Services, and


Broadcast receivers that can handle the Intent with help of specified set
of action, categories, data scheme associated with an Intent.

https://www.tutorialspoint.com/android/android_intents_filters.htm
Types of Intent
Explicit Intent:

• Explicit Android Intent is the Intent in which you explicitly define the
component that needs to be called by Android System.

• When you open an activity from another activity in the same Android app,
you use Explicit Intents.

Passing data/variable via Intent:


First Activity
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
Note: If you pass int then use
Second Activity extras.getInt(“variable”);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String sessionId= extras.getString ("EXTRA_SESSION_ID");
]
Animation: https://www.journaldev.com/9044/android-intent-handling-between-activities-example-
tutorial
Explicit Intents
Passing Object via Intent:

Serialization (Your class must implement Serialiable)


//FirstActivity
MyClass myObject= new MyClass(4, "Mustafa");
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("sampleObject", myObject);
startActivity(i);

//SecondActivity
Intent i = getIntent();
MyClass myObject= (MyClass) i.getSerializableExtra("sampleObject");

Gson Library
implementation 'com.google.code.gson:gson:2.8.5'
Gson gson = new Gson();
MyClass vp = new MyClass (5, “Ahmad”);
String myJson = gson.toJson(vp);
intent.putExtra("myjson", myjson);
//SecondActivity
Gson gson = new Gson();
MyClass ob = gson.fromJson(getIntent().getStringExtra("myjson"), MyClass.class);
Implicit Intent
• Implicit Intent is something which is sent from one activity to inbuilt android
activity in android.

• When we work with implicit intents, we generally specify the action which we
want to perform and optionally some data required for that action.

 Contains an action that any


other app (or its component)
can declare to handle

 Facilitates inter-application
communication and integration
How implicit Intent Works?
When using implicit intents, given such an arbitrary intent we need to know what to do
with it. This is handled by the process of Intent resolution, which maps an Intent to an
Activity, BroadcastReceiver, or Service (or sometimes two or more activities/receivers)
that can handle it.

The intent resolution mechanism basically revolves around matching an Intent against
all of the <intent-filter> descriptions in the installed application packages.

• There are three pieces of information in the Intent that are used for resolution:
1. the action (multiple predefined actions see docs)
2. Data & type (the data to operate on)
3. category.

• Using this information, a query is done on the PackageManager for a component that
can handle the intent.
• The appropriate component is determined based on the intent information supplied
in the AndroidManifest.xml
Implicit Intent Example

Call a number

Creating Alarm

Opening a Url from your app


Implicit Intent – Launch Camera and get Picture
What are pending intents?
A pending intent is a wrapper around regular intent that is designed to be used
by another application.

A PendingIntent provides a means for applications to work, even after their


process exits.
PendingIntent uses the following methods to handle the different types of
intents:
1. PendingIntent.getActivity(params) : Retrieve a PendingIntent to start an
Activity
2. PendingIntent.getBroadcast() : Retrieve a PendingIntent to perform a
Broadcast
3. PendingIntent.getService() : Retrieve a PendingIntent to start a Service

https://medium.com/@architgupta690/creating-pending-intent-in-android-a-step-by-step-guide-
Alarm Managers & Job Schedulers

Schedule jobs intelligently

Modern apps can perform many of their tasks asynchronously, outside the direct flow of
user interaction. Some examples of these asynchronous tasks are:
1. Updating network resources.
2. Downloading information.
3. Updating background tasks.
4. Scheduling system service calls.

https://www.javatpoint.com/android-alarmmanager
Alarm Managers & Job Schedulers
Alarm Manager (doesn’t work intelligently only at a specific time – out dated)

• By the help of Android AlarmManager in android, you can schedule your


application to run at a specific time in the future. It works whether your phone is
running or not.

• The Android AlarmManager holds a CPU wake lock that provides guarantee not to
sleep the phone until broadcast is handled.

//Alarm will launch our SecondActivity in 10 seconds


Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this.getApplicationContext(), 234324243, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ 10000, pendingIntent);
You should only use this API for tasks that must execute at a specific time, but don't require the other,
more robust, execution conditions that JobScheduler allows you to specify, such as device idle and
charging detect.

https://developer.android.com/topic/performa https://www.javatpoint.com/android-alarmmanager
Job Scheduler

The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the
JobScheduler class. This API allows to batch jobs when the device has more resources
available.

Example when you would use this job scheduler:


1. Tasks that should be done once the device is connect to a power supply
2. Tasks that require network access or a Wi-Fi connection.
3. Task that are not critical or user facing
4. Tasks that should be running on a regular basis as batch where the timing is not critical

Conditions:
You can schedule the task to run under specific conditions, such as:
1. Device is charging
2. Device is connected to an unmetered network
3. Device is idle
4. Start before a certain deadline
5. Start within a predefined time window, e.g., within the next hour
6. Start after a minimal delay, e.g., wait a minimum of 10 minutes

Code Example: http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html


Job Scheduler

For testing call this function


on onClick on a button

Code Example: http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html


Firebase Job Dispatcher

https://github.com/firebase/firebase-jobdispatcher-
android#user-content-firebase-jobdispatcher-

Home Exercise: Try to understand/ integrate it.

You might also like