How to send an arbitrary string via an Intent? - android

I'd like to send arbitrary strings (not paths or URLs) between two of my company's Android apps, but the Intent data is a URI. Presently I'm concerned with starting an Activity, but in some cases it would be nice to be able to send a query and receive a response without starting an Activity. So:
How should I send any arbitrary string when Intent wants a URI?
What other communication mechanisms could I use?

For Intent intent = new Intent();, you can use intent.putExtra(String name, String value); to store a String value that is keyed to name. In your called activity, use intent.getStringExtra(name); to retrieve the value.

You can add an arbitrary string to an intent by using extras on the intent.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(SOME_STATIC_NAME_VALUE, "your_arbitrary_string");
There are many options for extra's, see http://developer.android.com/reference/android/content/Intent.html
To retrieve the string in your receiving activity, use
intent.getExtras().getString(SOME_STATIC_NAME_VALUE)
Or the appropriate getter for whatever value you set on the extra.

Related

where does the data I passed using putExtra() method of intent to another activity get stored?

my Question is :
1 - If I send data to another activity where does it stored ?
2- what is the difference between put the parameter key of putExtra() method as builtin predefined string like EXTRA_TEXT and put a key with a random name
like "mymessage"
i.e
what is the difference between this code
public void go(View view)
{
String mytxt="hellow";
Intent i=new Intent(this , SecondActivity.class);
i.putExtra(Intent.EXTRA_TEXT , mytxt);
startActivity(i);
}
and this code
public void go(View view)
{
String mytxt="hellow";
Intent i=new Intent(this , SecondActivity);
i.putExtra("mydata" , mytxt);
startActivity(i);
}
3- how does android use the key part to of putExtra method to refer to my data
4- how does getExtra() method work from where it get the data
I think that 3 ans 4 are relevant by nature to the other above parts but I want to be clear about all my questions
thanks in advance
1 - If I send data to another activity where does it stored ?
I think there are a few different ways to answer this question, but the most straightforward is: it doesn't matter.
When you use an Intent to send data to another activity, what matters is that the other activity will receive an Intent object that is populated with the data you sent. The exact mechanism of how that happened is something you (as a developer) aren't supposed to care about.
The only exception to this that's worth mentioning is the fact that data in an Intent might (depending on exactly what you're doing) be subject to "Binder transaction size limits"; your data is serialized and transmitted at some point and, if the data is too large, this will fail.
2- what is the difference between put the parameter key of putExtra() method as builtin predefined string like EXTRA_TEXT and put a key with a random name like "mymessage"
There is no technical difference. In fact, Intent.EXTRA_TEXT is defined as a simple string itself ("android.intent.extra.TEXT").
The practical difference is that Intent.EXTRA_TEXT is a well-defined, known value that any developer can use. If I'm writing a chat program, and I want to let other apps open mine and hand off message text, I can tell users that I'll look for Intent.EXTRA_TEXT for this data, and it will be easy for everyone to use.
Within your own app, it really doesn't matter what strings you use for the key in a putExtra() call. As long as you use the same string later on to look it up, you can use anything you want. The only concern here is to make sure that you don't accidentally use the same key for two different values within the same Intent.
3- how does android use the key part to of putExtra method to refer to my data
It doesn't. All the android framework does is take a bunch of key-value pairs, serialize them (if necessary), and transmit them to another activity. That other activity, however, can then use the keys to look up the values. That's why e.g. Intent.EXTRA_TEXT is cool; it's a well-defined key that anyone can use to look data up, counting on callers on the other side to have put something in the map using that known key.
4- how does getExtra() method work from where it get the data
I guess you could say that the data comes from the app's memory.
You can think of the Intent object as being a really fancy Map<String, ?>. The same way you could write String s = (String) map.get("key"), you can write String s = intent.getStringExtra("key").
Your answers are as follow.
1. When we move from one Activity to another Activity, then data is passed using Intent and this data is sent to the system OS for safe keeping for restoration later, when we reach to another activity this data is restored from OS, There is a limit of 1 MB of this data, If It crosses then we get a crash
"TransactionTooLargeException" for nougat+ android devices.
2. Intent.EXTRA_TEXT is basically a predefined string in Intent class.
public static final String EXTRA_TEXT = "android.intent.extra.TEXT";
Google has coded internally for this key, suppose If we want to share a text with an application then we send this predefined key to send that "text" which need to be shared, thus It is a Standard Reserved key for which Google has coded internally to get and utilize that text
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "my sharign text here");
and on the other side, If we send any our key suppose "my_key" then we handle this on the other activity.
3/4.
When you pass on data from Activity 1 using putExtra() method, then all the data sent through putExtra() is packaged into a bundle, We can fetch this whole bundle in Activity2 using getExtras() method, or we can get our separate key value using getStringExtra(), getIntExtra(), getBooleanExtra() etc.
If you send extras to an activity, the extra data is not stored in a physical device like internal/external storage, but it is stored temporarily in RAM.
Intent.EXTRA_TEXT is a constant defined in the class Intent:
public static final String EXTRA_TEXT = "android.intent.extra.TEXT";
it is used with intents like sending emails and not passing data to activities, although it is not prohibited.
(and 4) The key part of the extra data is used to refer to the actual value of the data:
intent.putExtra("key1", "A");
intent.putExtra("key2", "B");
when the activity that opens wants to retrieve the extra values sent to it by the parent activity, it will check the keys:
if(getIntent().hasExtra("key1"))
String value1 = getIntent().getStringExtra("key1", "");

How to send specific id to intent picking contacts

I can pick multi contact data via
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
phonebookIntent.putExtra("FromMMS", true);
startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);
but when I want to add another contact to present data , the selected contact data disappears.Is there an intent filter like "selectedList" by which I can send id list to contact picker intent
That could be impossible to do using implicit intent. You can always create your own activity, get the contacts using ContentProvider, pass the parameters you want and handle the logic there.

Sending an intent without a string first parameter

Is it possible to send an intent to a second activity without filling out the first string parameter in intent.putExtra(String,var type) ?
Right now I have this:
intent.putExtra("Average Score: ", averageScoreToSend);
I don't want to send the string if I don't have to, but if I have to then I'll just deal with it.
Also how do I pull the double value from the sent intent? I don't understand what value goes in the second parameter
Intent i;
double averageScore = 0;
i = getIntent();
averageScore = i.getDoubleExtra(Grade1.FINALMATHGRADE, IDK);
I don't need to send the string at all if I don't have to
Yes, you do. The extras are a key-value store, and the first parameter is the key. You need the key to look up the value later on.
Also how do I pull the double value from the sent intent?
Call the appropriate get...Extra() method, passing in the key as the first parameter and the default value as the second parameter. The default value will be used in case the Intent does not contain an extra under the designated key.

Intent.putExtra vs. getJSONObject - Android

I have a question regarding the performance of an application.
I have an object in an external database and I have already extracted all the data about it from the database, including unique id. I start a new activity, but I want to use the same info for the same object in the new activity - what will be faster - include all the strings I already obtained via getJSONObject as extras for the intent or include just the object's id as an intent extra and make a new query to the database, receive the response and obtain the json response once again?
And how could I test what method is faster than the other?
Database query is a more expensive action than just passing the strings in the intent (especially if you pass the JSON string itself, not each string inside it) so if you're going to use the same data, I think you should pass it with the intent, however, this is not something that is hard to test and measure.

Providing EXTRA_AUTHORITIES when showing the ACTION_ADD_ACCOUNTS in order to show restricted account types to the user

The description for EXTRA_AUTHORITIES in android.provider.Settings says :
"This can be passed as an extra field in an Activity Intent with one or more syncable content provider's authorities as a String[]. This field is used by some intents to alter the behavior of the called activity.
Example: The ACTION_ADD_ACCOUNT intent restricts the account types available based on the authority given."
I want to show only the Corporate account type (or the activesync) to the user. I'm unable to find what String constants need to be passed as EXTRA_AUTHORITIES for this.
Can anyone point me to account type strings ? Or, provide an example of restrictively launching the add accounts page ?
Well, I hope I am not necroing anything, but you can add an authority for a content provider.
An example is in the LaunchActivity.java of the Android Calendar source, eg http://hi-android.info/src/com/android/calendar/LaunchActivity.java.html:
final Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(Settings.EXTRA_AUTHORITIES, new String[] {
Calendar.AUTHORITY
});
startActivityForResult(intent, 0);

Categories

Resources