What Is an Intent in Android

What is an Intent in Android?

An Intent is an "intention" to perform an action; in other words,

a messaging object you can use to request an action from another app component

An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly. Think of it as a blast email to a bunch of friends, in which you tell your friend John to do something, or to friends who can do X ("intent filters"), to do X. The other folks will ignore the email, but John (or friends who can do X) will react to it.

To listen for an broadcast intent (like the phone ringing, or an SMS is received), you implement a broadcast receiver, which will be passed the intent. To declare that you can handle another's app intent like "take picture", you declare an intent filter in your app's manifest file.

If you want to fire off an intent to do something, like pop up the dialer, you fire off an intent saying you will.

What's the difference between activity and intent in Android?

In very simple language, Activity is your user interface and whatever you can do with a user interface. When you move from one user interface, you need to launch that new user interface with an Intent. The Intent is your event that is passed along with data from the first user interface to another.

Intents can be used between user interfaces and background services too. Also an Intent is passed when you want to broadcast data to all activities and background services.

Intent lives as an object, activity lives with a face and interactions. Hope it has been helpful.

Why use Intents in Android?

Intents are get widely used in android to switch from one activity to other . it is good practice to use intents . Using intents we can pass/send values from one activity to another. So it can be used as value passing mechanism. Also its syntax is very simple.so why to think about threads ?

Differences between Intent and PendingIntent

Intent

An Android Intent is an object carrying an intent, i.e. a message from one component to another component either inside or outside of the application. Intents can communicate messages among any of the three core components of an application -- Activities, Services, and BroadcastReceivers.

The intent itself, an Intent object, is a passive data structure. It holds an abstract description of an operation to be performed.

For example: say you have an Activity that needs to launch an email client and send an email. To do this, your Activity would send an Intent with the action ACTION_SEND, along with the appropriate chooser, to the Android Intent Resolver:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this

The specified chooser gives the proper interface for the user to pick how to send your email data.

EXPLICIT INTENTS

// 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

// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));

// Starts Implicit Activity
startActivity(i);

Pending Intent

A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.

By giving a PendingIntent to another application, you are granting it
the right to perform the operation you have specified as if the other
application was yourself (with the same permissions and identity). As
such, you should be careful about how you build the PendingIntent:
almost always, for example, the base Intent you supply should have the
component name explicitly set to one of your own components, to ensure
it is ultimately sent there and nowhere else.

Example for Pending Intent : http://android-pending-intent.blogspot.in/

Source : Android Intents and Android Pending Intents

Hope this helps.

Knowing about Sticky intent in Android


Intent - is a message passing mechanism between components of Android, except for Content Provider. You can use Intent to start any
component.

Sticky Intent - Sticks with Android, for future broadcast listeners. For example if BATTERY_LOW event occurs then that Intent
will stick with Android so that any future requests for
BATTERY_LOW, will return the Intent.

Pending Intent - If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use
Pending Intent.

What are intent-filters in Android?


When there are multiple activities
entries in AndroidManifest.xml, how
does android know which activity to
start first?

There is no "first". In your case, with your manifest as shown, you will have two icons in your launcher. Whichever one the user taps on is the one that gets launched.

I could not understand intent-filters.
Can anyone please explain.

There is quite a bit of documentation on the subject. Please consider reading that, then asking more specific questions.

Also, when you get "application has stopped unexpectedly, try again", use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine the Java stack trace associated with the error.

What does intent.putExtra do?

When you start a new Intent, you may want to pass some information to it. putExtra is how you so that. For instance if you made an intent to display account details, you might pass it the account id that is to be displayed.
Basically any information you put in the extra bundle can be read later by the intent you've given it to.

The concept of an Intent in Android?

An Intent can be used to launch activities, by supplying an action and some data. An example of using an Intent action to view a web-page:

Intent myIntent = new Intent(Intent.VIEW_ACTION,
Uri.parse("http://www.google.com"));

Where the action is Intent.VIEW_ACTION and the data string is an Uri of Google's website.

Common Tasks and How To Do Them in Android

I have tried, but its tough to compare an Intent with something in everyday life. If I come up with something, I'll jot it down with my answer.

What is the purpose of android.intent.category.DEFAULT ?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter). If you are sure that your activity must be called with any other Category, don't use the Default.

Setting Category to Default doesn't mean that this Activity will be used by default when your app launches. The Activity just says to system that " Oh I could be started, even if the starter Intent's category is set to Nothing at all ! "

Different types of Android Intents

The first one is an implicit intent, while the second is an explicit intent.

The first one fired an Intent for the action com.example.project.MENU. If you look inside you project AndroidManifest.xml you can see some <intent-filter> balise. This baslise register activity, service or broadcast receiver to different actions.

This mecanism can be used to allow third party app to launch some of your activities.

You can see more on this tutorial http://www.vogella.com/tutorials/AndroidIntent/article.html#intenttypes



Related Topics



Leave a reply



Submit