Which function does intent have? [duplicate] - android

This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance

I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message

Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);

The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity

Related

What's the relationship between the Activity ,Action and Intent in Android?

I fell confused about the relationship between the Activity, Action and Intent?
And also an another question about the Implicit intents : for example I want to start this action ACTION_CALL to make a phone call,obviously, this is an Implicit intent, so I should write like this Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
But I should also need to set the data. Here is my question : How can I know what's the uri looks like? What's the schema?Please just don't tell the simple answer.How do you know that? Is there any API I should look for? And I know in the manifest <data android:scheme="xxxxx"/> So where is the manifest about the action or activity Intent.ACTION_CALL? I can't find it.
Activity - It says, from where to where you want to send message e.g: from Activity A to Activity B.
Action - It says, what this message means e.g: Open a URL in webview or make a call etc.
Intent - It says, how to transfer the message and other extra information e.g: directly through explicit intent or indirectly by selecting from multiple options through implicit intent.

How to redirect from own application another application?

I am creating my own app and at one point I want that user can go to any other application.So what will be the best code for that?
You can do that in many ways, but one of the common method is intents. You can create implicit intents for this purpose.
here is a sample code :
//From MainActivity.onCreate()
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://google.com"));
startActivity(intent);`
you can find further details about intents here.

How can I know the startActivity intent from other application?

I have an Activity and assume that has been launched. When the other application use startActivity method to start my Activity, my Activity will show and run the onResume Method, but I can't find any way to get the intent which is used in startActivity method by the other application. I want to get the extra data in the intent. How can I do?
EDIT
My Activity is singleTask, and I want to get the startActivity intents form other applications. I think it is not associate with filters.
Have you tried using getIntent() ?
Then you can do:
this.getIntent().getExtras();
After that if you need new intents just override the onNewIntent function in your activity.
I simply say an example. When we need to share something. We click share button which shows a list of app by which we can share our things.
So, if you want to make that kind of app which can receive other app data then you need make your activity capable of receiving that data. In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter in manifest.
Below this link you will get some more information : http://developer.android.com/training/basics/intents/filters.html

Android: getting info on the calling app from activity

I want to implement a fine-granularity protection mechanism in an exported activity. The permissions framework does not seem to work for my requirements.
There are two options I am considering:
using Activity.getCallingPackage - only works if the activity is started with startActivityForResult - this is a limitation I would like to avoid if possible.
using Binder.getCallingUid - when called in an Activity, it returns the local UID, and not the calling UID.
Is there any way to allow activities started with startActivity to retrieve any information about the calling app?
You can add extra information in your intent.
Intent i = new Intent(this, NextClass.class);
i.putExtra("extra", "This is some extra information";
startActivity(i);
You retrieve the data from NextClass by:
Intent i = getIntent();
String extraStuff = i.getStringExtra("extra");
Is there any way to allow activities started with startActivity to retrieve any information about the calling app?
No, sorry, beyond your startActivityForResult() hack. Android's support for "a fine-granularity protection mechanism" is designed around services, not activities or other components.

Question regarding account creation and sync on Android

I've been reading the sample code from the dev docs on Android's site, specifically this:
http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/authenticator/AuthenticatorActivity.html
Which is the sole activity of the sample app. It refers to an intent in the onCreate method. I don't understand where this intent is coming from, or what it should contain if this is the only activity the app utilizes.
Log.i(TAG, "loading data from Intent");
final Intent intent = getIntent();
mUsername = intent.getStringExtra(PARAM_USERNAME);
mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
mRequestNewAccount = mUsername == null;
mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
That's the block of code working with the intent. Why would you have an intent for the only activity in the app? Is this app called in an unusual way? The Manifest does not include an intent filter for the activity... I guess I'm just a bit lost on this whole thing! If someone could set me straight that'd be great, thanks.
Why would you have an intent for the only activity in the app?
getIntent() gets you the intent that started this activity.
Is this app called in an unusual way?
I guess this activity is called programmatically from another app or activity, since it has been passed some extra data: getStringExtra() is used to extract some data from the intent that started it. putExtra.. and getExtra.. is a way to pass data between activities when they are started.
In that specific example, the intent is sent from the addAccount method in Authenticator.java. That method is called by the OS when you click the Add Account button in the Accounts & sync settings screen and choose your account type.

Categories

Resources