I am trying to learn how to develop Android applications. I am reading on the Android Developers Guide site http://developer.android.com/guide/components/intents-filters.html, specifically about Explicit vs. Implicit Intents. One of the examples for an explicit intent looks like this:
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
The constructor Intent(this, DownloadService.class) looks like this public constructor in the Android APIs ( http://developer.android.com/reference/android/content/Intent.html ):
public Intent (Context packageContext, Class<?> cls)
The Intent constructor documentation states:
Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this.
What I am trying to figure out is, what is the "this" in the constructor? I mean, I know what "this" is in general (the current instance of your activity), but what is it in this specific content? In the comments, it says "Executed in an Activity, so 'this' is the Context", but what is the "Context"? It isn't one of the five pieces of information that make up an intent (Component Name, Action, Data, Category, Extras, Flags). I know the "DownloadService.class" parameter is the Component Name, so I'm just trying to figure out what it is.
What I am trying to figure out is, what is the "this" in the constructor?
It is an instance of some subclass of Context, such as an Activity.
It isn't one of the five pieces of information that make up an intent (Component Name, Action, Data, Category, Extras, Flags).
No, but the combination of the Context and the Java class is enough to build the ComponentName. A ComponentName is a combination of an application ID and the fully-qualified class name to the component in that application. The Java class can provide the fully-qualified class name; the Context is where ComponentName pulls in the application ID (in this case, for your own application).
Related
I am wondering how does the following code works (it starts an activity). I don't get how does the system figure out what is the action that should be preformed. No action is specified for the Intent. I would have expected a set_action.
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
I am wondering how is it possible to have an Intent which action is not explicitely specified considering what I read in the documentation:
The primary pieces of information in an intent are:
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
I hope it make sense. Thank you for your help.
There are two types of intents in Android, implicit intents and explicit intents.
1) Implicit intent
You set an Action, Category and data type and let Android find an activity that fits the specified characteristics (has an intent filter with the specified Action, Category and Data Type).
2) Explicit intent
As the docs says:
An explicit intent is one that you use to launch a specific app
component, such as a particular activity or service in your app. To
create an explicit intent, define the component name for the Intent
object—all other intent properties are optional.
You tell which activity/service to open explicitly. So the system doesn't need to figure out which one to open, you're already telling it to open a specific Activity/Service.
The one you read in the docs is an implicit intent, this is the explicit one:
Intent i = new Intent(this, ActivityTwo.class);
There are multiple constructor functions for Intent class.
If checking the source code for public Intent(Context packageContext, Class cls), the following info is mentioned:
Create an intent for a specific component. All other fields (action, data,
* type, class) are null, though they can be modified later with explicit
* calls. This provides a convenient way to create an intent that is
* intended to execute a hard-coded class name, rather than relying on the
* system to find an appropriate class for you;
For what purpose Context class is used in android?Please explain me in depth and be more specific .I read all other posts but none of them were specific enough to give me clear understanding.
I know Content class allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
Like Here,
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).
Similarly,
here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
There are already several good explanations of Context on Stackoverflow (see the linked questions and us the "search" feature. Also, the source code for Android is available on grepcode.com and you can look yourself if you are really interested. Why trust someone else's answer if you can look yourself? ;-)
However, I will answer your specific questions:
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor
call.what type of information does this activity context contain,how
will it help it ,what type of resource access is it providing to it
?(with example please).
In this case (the 2-argument constructor for Intent), the Context parameter is only used to determine the package name of the target Activity. Assuming that the package name of your application is "com.example.app", and MyActivity is an `Activity of your application, the following code snippets are all functionally identical:
Intent intent = new Intent(this, MyActivity.class);
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, "com.example.app.MyActivity");
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", MyActivity.class);
Intent intent = new Intent();
intent.setClass(this, MyActivity.class);
Intent intent = new Intent();
intent.setClassName("com.example.app", "com.example.app.MyActivity");
Similarly, here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
All Views need a Context. Think of the Context as the "owner of the View". This controls the lifetime of the View. In general, a View should have the same lifetime as its owning Activity, which is why you usually pass the Activity as the Context parameter when creating a View. When the Activity is destroyed, all of the owned Views are also destroyed. Additionally, the View uses the Context to gain access to resources (drawables, layouts, strings, themes, etc.).
It act as a Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
You can get all the information from delveloper's official documentation... https://developer.android.com/reference/android/content/Context.html
The Intent class had 6 constructors
Intent()
Create an empty intent.
Intent(Intent o)
Copy constructor.
Intent(String action)
Create an intent with a given action.
Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.
Intent(Context packageContext, Class cls)
Create an intent for a specific component.
Intent(String action, Uri uri, Context packageContext, Class cls)
Create an intent for a specific component with a specified action and
data.
I'm almost new in android programming and mostly using the fifth one when i need to start another Activity or Fragment:
Intent(Context packageContext, Class<?> cls)
When i want to start an Activity from a Fragment i do this:
Intent i = new Intent(getActivity(), DestinationActivity.class);
as far as i know, getActivity() will return an Activity
But the constructor expect a Context, how is this possible???
is it possible because of that the Activity that had returned by getActivity() implicitly invoke getApplicationContext()???
Take a look at the argument Context very closely in the fifth Intent declaration. It reflects polymorphism. The Intent takes a Context argument so you can pass any object that is a Context or derives from the Context class.
Activity, AppCompatActivity, IntentService, Service all derive from the Context class and hence can be passed as an argument to the method.
Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext(). You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.
You can find more details about the Activity class here.
This question about the intent constructor parameters is similar to yours and has a really good answer. I think you'd like to check it out.
Hope it helps.
Activity extends Context so you can just cast it:
Intent i = new Intent((Context)getActivity(), DestinationActivity.class);
I have recently started a new Android project and I'm working off the previous developer's code. I'm relatively new to Android and I've come across something that I'm unsure of.
What is the difference between this:
Intent intent = new Intent("com.example.project.MENU");
and this:
Intent intent = new Intent(this, DisplayMenu.class);
I understand what the 2nd code snippet does, I just can't get my head around as to what the first one is doing? Is it referencing the file in the package? Thanks
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
Basically an Intent carries some information that are used by the system in order to determine which component should be called for executing the action.
These information are:
Component name: the name of the component that should be launched. (If present the Intent is Explicit)
Action: it specifies the generic action that should be executed (es. ACTION_VIEW, ACTION_SEND). It determines how the rest of the intent is strucutred.
Data: represents the URI that refers to the object that should be associated with the action. For example with the action ACTION_EDIT, the Data should contain the URI of the document that you want modify.
Category: Additional infromation (for example if you want that your app is shown in the launcher you can use CATEGORY_LAUNCHER)
Extras: keys-values pairs that carries additional information
Flags: it is like a metadata that specify how the intent should be managed by the system.
The Intent class provides a lot of different constructors.
The first one you asked for is public Intent (String action)
So, this sets the Action, and lets null all other fields.
The second one public Intent (Context packageContext, Class<?> cls) creates an intent for a specific component by its Component name. All other fields are null. This is a Explicit Intent, since you declare exactly which component should receive it.
The first one is used when you need to call Intent from System
such as Open Camera, Gallery, or Share something to other Application
for example
// this one call Camera to Capture Image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// this one call gallery to let you select image
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
and That MediaStore.something here is just a Path to the system
for example
MediaStore.ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE"
Intent.ACTION_PICK = "android.intent.action.PICK"
The first type of intent is mostly used if you want to open another application from your application while the second type of intent is used to open another activity in your application.
For this code, 'localIntent' seems Explicit Intent directed to 'SecondActivity.class'. But it also seems that it's action has been set as 'SAMPLE'. In this case, is this still Explicit Intent? And when this type of Intent usually is used for?
Intent localIntent = new Intent(getApplicationContext(), SecondActivity.class).setAction("SAMPLE");
startActivity(localIntent);
For Intent of Android, there are only two types like this:
Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which
provides the exact class to be run. Often these will not include any
other information, simply being a way for an application to launch
various internal activities it has as the user interacts with the
application.
Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the
available components is best to run for that intent.
So, whether you set component or set class, that will be explicit intent.(read more here)