Android Intent : Android training in Chandigarh
Let us discuss about intents in android with android training in Chandigarh. An Android Intent is a question conveying an aim i.e. message
starting with one part then onto the next segment inside the application or outside the application. The goals can convey messages among any
of the three center segments of an application - exercises, administrations, and communicate beneficiaries. The goal itself, an Intent protest,
is a uninvolved information structure holding a dynamic portrayal of a task to be performed.
starting with one part then onto the next segment inside the application or outside the application. The goals can convey messages among any
of the three center segments of an application - exercises, administrations, and communicate beneficiaries. The goal itself, an Intent protest,
is a uninvolved information structure holding a dynamic portrayal of a task to be performed.
For instance, we should expect, that you have an Activity that necessities to dispatch an email customer and sends an email utilizing your
Android gadget. For this purpose,your Activity would send an ACTION_SEND alongside proper chooser, to the Android Intent Resolver.
The predetermined chooser gives the best possible interface for the client to pick how to send your email information.
Android gadget. For this purpose,your Activity would send an ACTION_SEND alongside proper chooser, to the Android Intent Resolver.
The predetermined chooser gives the best possible interface for the client to pick how to send your email information.
For instance, expect, that you have an Activity that requirements to open URL in a web program on your Android gadget. For this reason,
your Activity will send ACTION_WEB_SEARCH Intent to the Android Intent Resolver to open given URL in the web program. The Intent
Resolver at that point passes your page to the web program and begins the Web Browser Activity. There are separate instruments for conveying
goals to each kind of segment - exercises, administrations, and communicate recipients.
your Activity will send ACTION_WEB_SEARCH Intent to the Android Intent Resolver to open given URL in the web program. The Intent
Resolver at that point passes your page to the web program and begins the Web Browser Activity. There are separate instruments for conveying
goals to each kind of segment - exercises, administrations, and communicate recipients.
S.N. Method & Description
1 Context.startActivity() : The Intent question is passed to this technique to dispatch another movement or to get a current action to explore new territory.
2 Context.startService() : The Intent protest is passed to this strategy to start an administration or convey new guidelines to a continuous administration.
3 Context.sendBroadcast() : The Intent question is passed to this strategy to convey the message to all intrigued communicate recipients.
INTENTS & FILTERS
Intent Objects
An Intent question is a heap of data which is utilized by the segment that gets the expectation in addition to data utilized by the Android framework.
An Intent question can contain the accompanying segments in light of what it is imparting or going to perform Action This is compulsory piece of
the Intent protest and is a string naming the activity to be performed or, on account of communicate aims, the move that made place and is being
accounted for. The activity generally decides how whatever remains of the aim question is organized.The Intent Resolver parses through a rundown
of Activities and picks the one that would best match "The setData() technique indicates information just as a URI, setType() determines it just as a
MIME write, and setDataAndType() indicates it as both a URI and a MIME compose."
An Intent question can contain the accompanying segments in light of what it is imparting or going to perform Action This is compulsory piece of
the Intent protest and is a string naming the activity to be performed or, on account of communicate aims, the move that made place and is being
accounted for. The activity generally decides how whatever remains of the aim question is organized.The Intent Resolver parses through a rundown
of Activities and picks the one that would best match "The setData() technique indicates information just as a URI, setType() determines it just as a
MIME write, and setDataAndType() indicates it as both a URI and a MIME compose."
Some examples of action/data pairs are: S.N. Action/Data Pair & Description
1. ACTION_VIEW content://contacts/people/1 Display information about the person whose identifier is "1".
2. ACTION_DIAL content://contacts/people/1 Display the phone dialer with the person filled in.
3. ACTION_VIEW tel:123 Display the phone dialer with the given number filled in.
4. ACTION_DIAL tel:123 Display the phone dialer with the given number filled in.
Android
5. ACTION_EDIT content://contacts/people/1
Edit information about the person whose identifier is "1".
6. ACTION_VIEW content://contacts/people/
Display a list of people, which the user can browse through.
Category
The classification is a discretionary piece of Intent protest and it's a string containing extra data about the sort of part that should deal with the goal.
The addCategory() strategy puts a class in an Intent question, evacuate Category() erases a classification already included, and getCategories() gets
the arrangement of all classifications at present in the protest. Here is a rundown of Android Intent
Standard Categories. You can check detail on Intent Filters in beneath segment to see how would we utilize classifications to pick fitting movementThe addCategory() strategy puts a class in an Intent question, evacuate Category() erases a classification already included, and getCategories() gets
the arrangement of all classifications at present in the protest. Here is a rundown of Android Intent
comparing to an Intent.
Extras
set and read using the putExtras() and getExtras() strategies independently. Here is an once-over of Android Intent Standard Extra Data.
Flags
It teach the android framework how to dispatch an action, these flags are discretionary piece of intent protest, and how to treat it after it is propelled
and so forth.
and so forth.
Component Name
It is an android ComponentName protest speaking to either Activity, Services or BroadcastReceiver class. In the event that it is set, the Intent protest is
conveyed to an occasion of the assigned class, generally Android utilizes other data in the Intent question find a reasonable target. The part name is
set by setComponent(), setClass(), or setClassName() and read by getComponent().
conveyed to an occasion of the assigned class, generally Android utilizes other data in the Intent question find a reasonable target. The part name is
set by setComponent(), setClass(), or setClassName() and read by getComponent().
Types of Intents
There are following two types of intents supported by Android till version 4.1
Android Explicit Intents
For example:
// Explicit Intent by specifying its class name
Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts TargetActivity
startActivity(i);
Implicit Intents
These intents do not name a target and the field for the component name is left
blank. Implicit intents are often used to activate components in other
applications. For example:
// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
// Starts Implicit Activity
startActivity(i);
The target component which receives the intent can use
the getExtras() method to get the extra data sent by the source component.
For example:
// Get bundle object at appropriate place in your code
Bundle extras = getIntent().getExtras();
// Extract data using passed keys
String value1 = extras.getString("Key1");
String value2 = extras.getString("Key2");
Example: Following is the example that shows the functionality of android intent applications which android build-in applications.
Step Description
1 You will use Eclipse IDE to create an Android application and name it as
IntentDemo under a package com.example.intentdemo. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
2 Modify src/MainActivity.java file and add the code to define two listeners
corresponding two buttons i.e. Start Browser and Start Phone.
3 Modify layout XML file res/layout/activity_main.xml to add three buttons
in linear layout.
4 Modify res/values/strings.xml to define required constant values.
5 If you want to launch the android emulator applications, verify the result of changed done in application.
Following is the content of the modified main activity file
src/com.example.intentdemo/MainActivity.java.
package com.example.intent demo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Android super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBrowser = (Button) findViewById(R.id.start_browser);
startBrowser.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
startActivity(i);
}
});
Button startPhone = (Button) findViewById(R.id.start_phone);
startPhone.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("tel:9510300000"));
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
// bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"