How can I extract all information about an intent? Like action, extras, bundles etc.
Intent intent = getIntent();
Log.d(Tag.getTag(this), (/*What shall I add here?*/));
intent.getAction(), intent.getData(), intent.getExtras(), and maybe others. All of these are documented in the Intent class.
If you are using toString() method, you can see something like,
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.test/.TestDeleteActivity }
What I suggest is, if you want details about your running Intent, use the appropriate methods. As an example by using getPackage() method you can retrieve the application package name this Intent is limited to. The documentation contains all the methods you can use with Intent class.
If you want to pass some data from one Intent to another, use the Bundle class. This link will show you a simple example of how to use Bundle class.
Related
An Intent is a passive data structure that carries information from one Activity to another. An Intent is also capable of holding data in the form of name-value pairs (via putExtra()).
But while overriding the onCreate() method we pass a Bundle as the parameter, which ultimately also holds values in the form of name-value pairs and is able to store information with the help of onSaveInstanceState().
In such a scenario why do we need both and what differentiates the two?
I suppose I have led you guys into a misbelief that I have misunderstood what an Intent is:
When I said "An Intent is a passive data structure that carries information from one Activity to another", what I intended to point out was that even an Intent can carry information (other than the context and action description) with the help of putExtra() method. Why do we need to use a Bundle then?
I think you already have understood what a Bundle is: a collection of key-value pairs.
However, an Intent is much more. It contains information about an operation that should be performed. This new operation is defined by the action it can be used for, and the data it should show/edit/add. The system uses this information for finding a suitable app component (activity/broadcast/service) for the requested action.
Think of the Intent as a Bundle that also contains information on who should receive the contained data, and how it should be presented.
From the source of Intent class, there really is no difference between the two. Check below code from Intent class:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
And
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
So I think, only difference is ease of use.. :) for 1st, you don't need to create your bundle explicitly.
Intent facilitate communication between components.Intent is the message that is passed between components such as activity.
that can be used intent.putExtra(Key,value) and intent.putExtra(Bundle)
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
// intent.putExtra(key,value)
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);
Using Bundle : For example
Bundle bundle=new Bundle();
bundle.putString("Key","Some value");
intent.putExtras(bundle);
startActivity(intent);
Call the bundle in another activity :
Bundle extras=getIntent().getExtras();
extras.getString(key);
I really don't know from where you got this definition for Intent, but as an 'Intent' definition
An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver
components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding
between the code in different applications. Its most significant use
is in the launching of activities, where it can be thought of as the
glue between activities. It is basically a passive data structure
holding an abstract description of an action to be performed.
So Intent is an action to link to new (Activity, Service, BroadCastReceiver)
In Intent you will find a definition for Extras
extras -- This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
So that means Extras in the Intent is an object of A Bundle
Going to Bundle as you mentioned it is a carrier for data from one Intent to another and is a map of Key-Value variables.
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 have some values and want to pass with activity so that I can show in TextViews of Activity, I am unable to understand such a concept, so what should I do?
Simply I want to make constructor but unable to understand it that how will it be done, I am new to android programming, so needed help.
Start by reading the documentation at developer.android.com about intents and intent extras.
In android if you launched an activity there is a method called onCreate execute automatically.You can send values using Intent to the activity and can retrieve them in the Activity
The Activity (sub)class must have a default constructor without any parameters so that the system can instanciate it at run time.
To pass "parameters" to activities, you need to use the extra bundle of the intent.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.sample.MyParameter", 666);
startActivity(i);
See Starting An Activity
If you want to pass one value then you can use intent but if you want to pass multiple values then using "Bundle" is best way.
Bundle bundel = new Bundle();
bundel.putStringArray("key1",strings);
bundel.putStringArray("key2",stringsofids);
bundel.putString("key3", str31);
bundel.putStringArray("key4",stringsbakup);
bundel.putString("key5", str1);
bundel.putString("key6", str4);
I have one activity in one application, which is supposed to run another activity in another application, while passing to it an arguments|(Through putExtra)
The problem is that I know that I must add something like:
Intent i = new Intent(target);
i.setAction("actionstring" + System.currentTimeMillis());
to the target intent(activity) so the system wont override the Extra's and ill get diffrent extra's each time i run the target activity.
But when i add the setAction i get an error:
couldnt find activity: No Activity found to handle Intent { action=actionstring1278829343752 flags=0x10000000 (has extras) }
Any idea how could I solve it with setAction? maybe something I have to add also to the manifest?
You can not use setAction for what you're trying to achieve, you must use [putExtra][1].
[1]: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, java.lang.String[])
try First Activity:
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity:
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);
Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
}
});
is there a way to pass those arguments in the calling ?
can someone show an example explaining
what send activity should do
what the new born activity should do
As ben already mentioned you can add this data to the intent inside an extra bundle.
An extra bundle stores data as a key value pair of native java data types.
You can add data to the intent via the putExtra methods.
In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.
To get the intent that started the current activity just use the getIntent() method from activity.
You have to use extras like this:
i.putExtra(varName, value);
Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...
fine.. U can also use StartActivity(intent); Thats enough to pass