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.
Related
Everyone knows if you create intent to start another activity, you pass in as a parameter into startActivity. But I just thought about the possible scenario: intent says system "call this activity", the system sees manifest and then runs activity, or this running acts internally in the app, something like "call some method of some class"?
Probably a stupid question, but I couldn't find enough info. So how does it works?
Following is the way how intent communication works:
Activity A creates an Intent with an action description and passes it to startActivity().
The Android System searches all apps for an intent filter that matches the intent. When a match is found,
the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.!
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
I m creating an app which opens another app in the background and my first app will be sending some string as a data to another app, so can I send Intents to another app's like we usually send intents to other classes containing data?
If we can then how can I send it?
Yes , you can send the intent to any app you like but its upto the receiving application to handle it.
Few apps may crash receiving it.
The way
Make an intent
Intent i=new Intent(yourContext,Activity_to_which_you_to_send.class);
Put some data-if you want to
i.putExtraString("key","value");
or put using a bundle
Bundle b=new Bundle();
b.putString(key,boolean_value);
b.putBoolean(key,boolean_value)
Starting the activity
startActivity(i);
Set the package of the app
i.setPackage("com.whatsapp");
Example
if you want to find out the main Activity of an app
go to command line
type adb shell pm -lf
pick any one and try it by passing as a second argument to the intent constructor defined above and then call startActivity method.
hope it helps you.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.putExtra(name,yourString);
try{
startActivity(intent);
} catch (Exception ActivityNotFoundException){
ActivityNotFoundException.printStackTrace();
}
Use this code to send an intent to another app and specify the name value and string in putExtra function.
Yes you can send Intent to another app like we send to other classes with passing data as a string so you can see it HERE
Yes. "App" is not so well defined on Android, it's a loose term. Intents are used to start an Activity, among other things.
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.
In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.