AAD 4 (Part 2) SJ
AAD 4 (Part 2) SJ
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.
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);