0% found this document useful (0 votes)
22 views40 pages

AAD 4 (Part 2) SJ

Uploaded by

Rohita Yamaganti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views40 pages

AAD 4 (Part 2) SJ

Uploaded by

Rohita Yamaganti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Intent Filters

• The intent is “Explicit Intent” with the component name


field set, then it’s delivered to the target component. But
in “Implicit Intent” which is typical for
communication between applications, android must
determine the matched activity or service (or set of
receivers) on its own. How does Android do that? That’s
where intent filters come in.
• Let see the example you build apps for play videos and
published store. Users installed and using it’s. But if
someone has a video and clicked it, so does your
application will launch and play this video? No, if you’d
didn’t specify about the app component in intent filters.
• Intent Filter describe about the component that
can handle the intent.
Manifest File:
<activity android:name ``.MainActivity”>
<intent-filter>
<action android:name=
“android.intent.action.SEND”>
<category android:name=
“android.intent.category.HOME”>
<data android:mimeType= “text/plain”>
</intent-filter>
</activity>
Multiple Intent filters in Manifest.xml
• Multiple Intent Filters can also be present in a single Manifest file, as
follows:
<activity android:name ``.MainActivity”>
<intent-filter>
<action android:name= “android.intent.action.SEND”>
<category android:name= “android.intent.category.HOME”>
<data android:mimeType= “text/plain”>
</intent-filter>
</activity>
<activity android:name ``.MyActivity”>
<intent-filter>
<action android:name= “android.intent.action.EDIT”>
<category android:name= “android.intent.category.ALTERNATIVE”>
<data android:mimeType= “text/plain”>
</intent-filter>
Let’s build one simple Intent filters example
• Step 1. Create a new project “Build Your
First Android App in Java“
• Step 2. Add Button widget in the
main_activity.xml resource layout file.The
button is for performing an operation.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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: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_marginTop="8dp"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="72dp"
android:text="Go To Tutorial"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>
• Step 3. Add the following code in
MainActivity.class
Set on Click listener in button
and Create Intent instance
Pass the Action Intent.ACTION_VIEW and set
Data Uri.parse(url).
package in.eyehunt.intentfilters;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.button);


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "https://tutorial.eyehunts.com";
//action
Intent i = new Intent(Intent.ACTION_VIEW);
//data
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}
• Now when you click the button its send intent
in an Android system where it will search
matching component based on Intent filters.
If there is more then one result then it will show
multiple option otherwise open on only one.
• Step 6.
Run the application, in an emulator or On
you android device
• Output screenshot Android Intent
Filters example :
Intent Resolution

The intent resolution comes in the role when the system receives an implicit
intent. Implicit intents do not mention the name of the components.
Therefore the system needs to search for the appropriate application
component that can perform the actions. This searching process takes place
in the following three tests:

1. Action Test
• In this, the system checks the Intent action matches with the intents in the
intent filter. If it does, it passes this test, otherwise, it fails.

2. Category Test
• In this, the system checks the Intent category matches with the categories in
the intent filter. If it does, it passes this test, otherwise, it fails.

3. Data Test
• In this, the system checks the Intent MIME data matches with the data in
the intent filter. If it does, it passes this test, otherwise, it fails.
Example
BroadcastReceiver
BroadcastReceiver is used to respond to
broadcast messages from other applications or by
android system.
Example:
Some apps are closed automatically when battery
is low
(LOW_BATTERY is a broadcast message from
android system…when it is received…the app is
responding to the event…and closing)
What is Android Broadcast Receiver?

https://data-flair.training/blogs/android-broadcast-receiver/
System-generated Intents
Steps to create a BroadcastReceiver

• Create a BroadcastReceiver
Public class MyReceiver extends
BroadcastReceiver{
public void onReceive(context,intent){
…..respond to broadcast message..
}
}
• Register broadcastReceiver
<receiver android:name=".MyReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
Toast
 In android, Toast is a small popup notification that is used to display an
information about the operation which we performed in our app. The Toast will
show the message for a small period of time and it will disappear automatically
after a timeout.
 The size of Toast will be adjusted based on the space required for the
message and it will be displayed on the top of the main content of activity for a
short period of time.
 For example, some of the apps will show a message like “Press again to exit” in
toast, when we pressed a back button on the home page or showing a message
like “saved successfully” toast when we click on the button to save the details.
Create a Toast in Android
• In android, we can create a Toast by instantiating
an android.widget.Toast object using makeText() method.
The makeText() method will take three parameters: application context, text
message and the duration for the toast. We can display the Toast notification by
using show() method. Following is the syntax of creating a Toast in android
applications.
Android Toast Notification Example
Output of Android Toast Notification Example
Change the Position of Android Toast Notification
Output of Android Toast Positioning Example

https://www.tutlane.com/tutorial/android/android-toast-with-
examples
Notifications
A notification is a message you can display
to the user outside of your application's
normal UI. When you tell the system to issue
a notification, it first appears as an icon in
the notification area. To see the details of
the notification, the user opens the
notification area by dragging down from
notification area.
Create Notification buider
As a first step is to create a notification builder
using NotificationCompat.Builder. You will use
Notification Builder to set various Notification
properties like its small and large icons, title,
priority etc.

NotificationCompat.Builder mBuilder = new


NotificationCompat.Builder(this)
Setting Notification properties
Once you have Builder object, you can set its Notification properties using
Builder object as per your requirement. But this is mandatory to set at least
following −

A small icon, set by setSmallIcon()

A title, set by setContentTitle()

Detail text, set by setContentText()

mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Issue the notification
Finally, you pass the Notification object to the system by calling
NotificationManager.notify() to send your notification. Make sure
you call NotificationCompat.Builder.build() method on builder
object before notifying it. This method combines all of the options
that have been set and return a new Notification object.

NotificationManager mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

// notificationID allows you to update the notification later on.


mNotificationManager.notify(notificationID, mBuilder.build());
Creating and displaying Notifications
Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy