Tuesday 12 February 2013

Imlicity Intent concept and its example


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imlicity Intent


In last section  we had explained the concepts of intent understanding intent in android. Now, we will move on to a more interesting concept of Implicit Intents and Intent Filters. The code of this post can be found at end.
As described earlier, an implicit intent does not name a target component that should act upon the intent. Android resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?
Basically, an Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:
  • Action
  • Category
  • Data
So, Android compares the three (action, category and data) to something called Intent Filters that are declared by probable target components who are willing to accept Implicit Intent calls. i.e. Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.
So here are some important points to remember:
  1. Implicit Intents do not specify a target component.
  2. Components willing to receive implicit intents have to declare their ability to handle a specific intent by declaring intent filters.
  3. A component can declare any number of Intent Filters.
  4. There can be more than one component that declares the same Intent Filters and hence can respond to the same implicit intent. In that case, the user is presented both the component options and he can choose which one he wants to continue with.
  5. We can set priorities for the intent filters to ensure the order of responses.
There are 3 tests conducted in order to match an intent with intent filters:
  1. Action Test
  2. Category Test
  3. Data Test
Finally, we’ll look at declaring an implicit intent in one activity which will invoke one of the native activities of the platform by matching the intent filters declared by the same.
The ImplicitIntent Activity creates an implicit intent object contacts. This intent object’s component is not set. However, the action is set to android.content.intent.ACTION_VIEW and the data’s URI is set toPeople.CONTENT_URI.
Such an intent matches with the intent filter declared by the view contacts native activity.
So, when we run this application, it displays the native UI for viewing the existing contacts on the phone!
Here is the relevant piece of code for the same:
Button viewContacts = (Button)findViewById(R.id.ViewContacts);
viewContacts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});
In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.
Here are our Java code, ImplicitIntent.java:
package com.bogotobogo.implicitintent;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ImplicitIntent extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewContacts();
}
private void ViewContacts() {
try {
Button viewContacts = (Button)findViewById(R.id.ViewContacts);
viewContacts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});
}catch (ActivityNotFoundException anfe) {
Log.e(“ViewContacts”,”Viewing of Contacts failed”, anfe);
}
}
}

Some Important Topic Of Android With There Practical Example

1- What is Broadcast Receiver in android with example
2-Important android interview questions and answer
3- What is Fragment and it practical example
4-What is Service with an example
5- What are intents in android
6- What is Android
7-What is Activity , Life Cycle and its Practical example
8-Android Fragments with daynamic Fragment example
9-What is Loaders , component, features of Loader in android
10-what is Linux kernel
11-Types of Kernel
12-COMPILATION AND PROJECT BUILDING IN ANDROID
13-Google Map integration in android application 


No comments:

Post a Comment