0% found this document useful (0 votes)
26 views33 pages

Unit - V Activity and Multimedia With Databases Unit Outcome

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 33

Unit -V

Activity And Multimedia with databases


Marks-20

Unit Outcome-
a. Apply the given Intents and service in Application development.
b. Use Fragment to generate the given multiple activities.
c. Develop programs to play the given multimedia.
d. Write the query to perform the given database management operation
Intent
Android uses Intent for communicating between the components of an Application and also from one application
to another application.
Intent are the objects which is used in android for passing the information among Activities in an Application and
from one app to another also. Intent are used for communicating between the Application components and it also provides
the connectivity between two apps.
In android, Intent is a messaging object which is used to request an action from another app component such
as activities , services , broadcast receivers, and content providers.
Generally, in android, Intents will help us to maintain the communication between app components from the
same application as well as with the components of other applications.
Android intents are mainly used to:
• Start the service
• Launch an activity
• Display a web page
• Display a list of contacts Broadcast a message Dial a phone call etc.
Uses of Intent in Android
There are three fundamental uses of intents:
1. To start an Activity
An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent
to startActivity(). The Intent describes the activity to start and carries any necessary data along.
2. To start a Service
A Service is a component that performs operations in the background and does not have a user interface. You
can start a service to perform a one-time operation(such as downloading a file) by passing an Intent to startService(). The
Intent describes which service to start and carries any necessary data.
3. To deliver a Broadcast
A broadcast is a message that any app can receive. The system delivers various broadcasts for system events,
such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an
Intent to sendBroadcast() or sendOrderedBroadcast()..
In android, Intents are the objects of android.content.Intent types and intents are mainly useful to perform the
following things.

Component Description
Starting an Activity By sending an Intent object to startActivity() method we can start a new Activity or
existing Activity to perform required things.

Starting a Service By sending an Intent object to startService() method we can start a new Service or send
required instructions to an existing Service.

Delivering a Broadcast By sending an Intent object to sendBroadcast() method we can deliver our messages to
other app broadcast receivers.
Types of Intents
In Android, there are two types of Intents:
• Explicit Intents
• Implicit Intents
1.Explicit Intents-
-Explicit Intents are used to connect the application internally.
-Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another
activity in android by explicit intent.
-We can also pass the information from one activity to another using explicit intent.
-By using explicit intents we can send or share data/content from one activity to another activity based on our
requirements.
- For Example: If we know class name then we can navigate the app from One Activity to another activity using
Intent. In the similar way we can start a service to download a file in background process.
Create an Explicit Intent
To create an explicit intent,
1. You need to make an Intent object. The constructor of the Explicit Intent's object needs two parameters as follows:
Context c: This represents the object of the Activity from where you are calling the intent.
Java file name: This represents the name of the java file of the Activity you want to open.
Example-
Intent i = new Intent(this, MyJavaFile.class);
2. Call startActivity() method and pass the intent's object as the parameter. This method navigates to the java file mentioned
in the Intent's object.
startActivity(i);
3. If you need to pass some information or data to the new Activity you are calling, you can do this by calling putExtra()
method before the startActivity() method. This method accepts key-value pair as its parameter.
i.putExtra("key1", "I am value1");
i.putExtra("key2", "I am value2");
startActivity(i);
4. To receive the data in the new Activity and use it accordingly, you need to call the getIntent() method and then
getStringExtra() method in the java class of the Activity you want to open through explicit intent. getStringExtra() method
takes the key as the parameter.
String a = getIntent().getStringExtra("key1");
Doing this, stores the value stored at key1 into the string variable a.
Example-
Example-
Example-
Example-
Implicit Intents-
-In Implicit Intents we do need to specify the name of the component. We just specify the Action which has to be
performed and further this action is handled by the component of another application.
-An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an
implicit intent is useful when your app cannot perform the action, but other apps probably can and you'd like the user to pick
which app to use.

Example-
1. The basic example of implicit Intent is to open any web page
2. For example, if you want to show a specific location of the user on a map, you can use an implicit intent to pass
the coordinates through the intent and then any other app, which is capable of showing the coordinates on a map will accept
that intent.
Create an Implicit Intent-
To create an implicit intent,
1. You need to make an Intent object. The constructor of the Implicit Intent's object needs a type of action you want to
perform.
ACTION_VIEW: This action is used when you have some information that an activity can show to the user, such as a
photo to view in a Gallery app, or an address to view in a Map app.
ACTION_SEND: This action is used when you have some data that the user can share through another app, such as
an Email app or some Social Networking app.
ACTION_DIAL :Display the phone dialler with the given number filled in.

Example-
Intent i = new Intent(Intent.ACTION_VIEW);
2. You need to provide some data for the action to be performed. Data is typically expressed as a URI(Uniform Resource
Identifier) which provides data to the other app so that any other app which is capable of handling the URI data can perform
the desired action. For example, if you want to open a website through your app, you can pass the Uri data using setData()
method as follows:
i.setData(Uri.parse("http://www.google.co.in"));

3. Call startActivity() method in the end with the intent object as the parameter.
startActivity(i);
Example-
phone call in android Example-
Some extra Example-
1. //perform Call open action
@Override public void onClick(View v) {
// AndroidManifest.xml -> Permissions -> Add -> // Uses Permission -> android.permission.CALL_PHONE -> Save
Intent i = new Intent();
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:"+et.getText()));
startActivity(i); } });
2. //perform Dial open action
@Override public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:"+et.getText()));
startActivity(i); }});
3. Gallery open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://media/external/images/media/"));
startActivity(i); } });
4. perform CallLog open action- For Permission- <uses-permission android:name="android.permission.READ_CALL_LOG" />
@Override
public void onClick(View v)
{ Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://call_log/calls/1"));
startActivity(i); } });
5. perform Contact open action - <uses-permission android:name = "android.permission.READ_CONTACTS" />
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people/"));
startActivity(i); } });
 Intent Filter-
Intent filters are helpful while using implicit intents, It is not going to handle in java code, we have to set
it up in AndroidManifest.xml. Android must know what kind of intent it is launching so intent filters give the
information to android about intent and actions.
Intent Filter are the components which decide the behaviour of an intent. As we have read in our previous tutorial
of Intent about the navigation of one activity to another, that can be achieve by declaring intent filter. We can declare an
Intent Filter for an Activity in manifest file.
Intent Filter Code Inside Android Manifest:
 Syntax of Intent Filters:
Intent filter is declared inside Manifest file for an Activity.

<!--Here Name of Activity is .MainActivity, image of icon name stored in drawable folder, label present inside string name is label-->

<activity android:name=".MainActivity">
<intent-filter android:icon="@drawable/icon"
android:label="@string/label" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
 Attributes of Intent Filter:
Below are the attributes of Intent filter:
1. android:icon-
An icon represents the activity, service or broadcast receiver when a user interact with it or when it appears to user
in an application. To set an icon you need to give reference of drawable resource as declared android:icon=”@drawable/icon”.
How to add custom icon in your App:
Step 1: Open your android project folder in computer / system
Step 2: Open app>> src >> main >> res >> drawable >> here save custom icon image with name
Step 3: Replace @drawable/icon with @drawable/your_image name in the below code
android:icon="@drawable/icon"
2. android:label
A label represents the title of an activity on the toolbar. The label should be set as a reference to a string resource.
However, you can also use a raw string to set a label as declared in the below given code snippet
android:label = "@string/label"
or
android:label = "New Activity"
How To Create Custom Label in App:
Step 1: Click on values values
Step 2: Open Strings.xml file present inside values
Step 3: Edit String value to your custom name.
For example, we put AbhiAndroid.
Below is sample code of Intent filter for an App having two activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<!--Add code here for first activity-->
</intent-filter> </activity>
<activity android:name=".Main2Activity">
<intent-filter>
<!--Add code here for second activity-->
</intent-filter> </activity> </application>
Elements In Intent Filter:
There are following three elements in an intent filter:
1. Action
2. Data
3. Category
Important Note: Every intent filter must contain action element in it. Data and category element is optional for it.
1. Action:
It represent an activities action, what an activity is going to do. It is declared with the name attribute as given below
<action android:name = "string" />
An Intent Filter element must contain one or more action element. Action is a string that specifies the action to
perform. You can declare your own action as given below. But we usually use action constants defined by Intent class.
<intent-filter>
<action android:name="com.example.android.intentfilters.Main2Activity"/>
</intent-filter>
There are few common actions for starting an activity like ACTION_VIEW

ACTION_VIEW: This is used in an Intent with startActivity(). This helps when you redirect to see any website, photos in gallery
app or an address to view in a map app.

For example: As we have done in previous topic of Intent, we started a website using implicit intent in that Intent we used
ACTION_VIEW element to view website in the web browser.
Intent intentObj = new Intent(Intent.ACTION_VIEW);
intentObj.setData(Uri.parse("https://www.abhiandroid.com"));
startActivity(intentObj);

There are more actions similar to above like, ACTION_SEND, ACTION_MAIN, ACTION_WEB_SEARCH and many more.
2. Data:
There are two forms in which you can pass the data, using URI(Uniform Resource Identifiers) or MIME type of data.
The syntax of data attribute is as follows:
<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />
3. Category: This attribute of Intent filter dictates the behaviour or nature of an Intent.
The syntax of category is as follows:
<category android:name="string" />
category for example: CATEGORY_BROWSABLE, CATEGORY_LAUNCHER, CATEGORY.DEFAULT
BROWSABLE – Browsable category, activity allows itself to be opened with web browser to open the reference link provided
in data.
LAUNCHER – Launcher category puts an activity on the top of stack, whenever application will start, the activity containing
this category will be opened first.

<intent-filter>
<!--Code here-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<!--Code here-->
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Example- Intent filter

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
Example- Multiple Intent Filters in Manifest File
<activity android:name=".MainActivity">
<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity android:name=".ResultActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter> </activity>
Example-2

You might also like