Bundle extras = intent.getExtras();
if (extras != null) {
Toast.makeText(context, "Message recieved", Toast.LENGTH_SHORT).show();
}
What is the value stored in extras.. :?
The values stored in extras are the values you put into the extras.
To add an extra to an intent, do the following before you start it.
intent = new Intent(v.getContext(),TextActivity.class);
intent.putExtra("Title", "I am An extra");
startActivityForResult(intent, -1);
Then in your intent, to read it do:
String title = getIntent().getStringExtra("Title");
The code in your question is just posting a popup message if there is an extra found.
Currently you do not add anything to extras.
Extras is a Bundle, so it'll usually hold a collection of values. From your code fragment, it is impossible to tell what is in there. It depends on what the code that created the intent put into the bundle.
If you want to know all keys in a Bundle, use Bundle.keySet().
Regarding your remark, there is no true "beginning of a program" in an Android application. Your activity is marked in the manifest as the 'launcher' activity. If your activity is started from the Launcher, the Extras will be empty. However, no-one is stopping you (or other applications) from starting your activity manually, providing data in the extras.
There is no magic involved here. If you don't put anything into the Extras, nothing comes out.
Related
On my Application level I receive null for getExtras(), but on Activity level i can see them correctly.
public class MyApplication extends Application
{
#Override
public void onCreate() {
super.onCreate();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
if (intent != null){
String mStaticWorldUrl = intent.getStringExtra("arg1Name");
String mStaticWorldIconUrl = intent.getStringExtra("arg2Name");
Log.i("LOG", mStaticWorldUrl + " --- " + mStaticWorldIconUrl);
}
}
}
I'm calling the app from some shortcuts that were created by this code:
(- each shortcut has different Extras sent to the Intent)
// create a shortcut for the specific app
public static void createShortcutForPackage(Context context,
String packageName, String className, String shortcutName,
String arg1Name, String arg1Val, String arg2Name, String arg2Val,
int iconID) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
Context pkgContext = createPackageContext(context, packageName);
if (pkgContext == null)
return;
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = pm.getLaunchIntentForPackage(packageName);
if (arg1Name != null)
shortcutIntent.putExtra(arg1Name, arg1Val);
if (arg2Name != null)
shortcutIntent.putExtra(arg2Name, arg2Val);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, iconID));
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
how can I read these Extras on the Application level?
or is there any other way to create different shortcuts for application and read its' "parameters" data on Application?
The Application class is static for the application: there is only ever a single instance of it for your app's process. If your app has been launched with a normal launch Intent, rather than a shortcut you created, then no extras would be present. The app process does not die when HOME or BACK is pressed, so the Intent used to launch the package may not be what you think it should be.
You should not need to look at the Intent at the Application level. Intent objects are not intended to be "sent" there, but rather to an Activity, Service or BroadcastReceiver.
This is conceptual error which facing to get data in application class using getExtra which is the method of Intent
Reason for this issue:
No extras is because the queried intent was one that was generated
one line earlier, it is the intent that the OS has generated for the
package as requested in the following code: Intent intent =
getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
Intent objects within an Application class instance: they are not
delivered there
Let's understand following things to use in upcoming usage while anyone want to get data in application level
What is Intent ?
What is the Use of Intent?
What other things can we use to achieve this?
What is Intent?
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.
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.
What is the Use of Intent?
Use to intents facilitate communication between components in several ways, Followings are standard use
To start an activity.
To start a service.
To deliver a broadcast
What other things can we use to achieve this?
There are lots of things we can use to achive this and solved this
issue.
But right now here i mentioned only one which is standard and secured
to use application
Content provider: To offer a file from your app to another app is to
send the receiving app the file's content URI and grant temporary
access permissions to that URI. Content URIs with temporary URI
access permissions are secure because they apply only to the app that
receives the URI, and they expire automatically.
I am working on a download manager . for managing notifications every second it calls two methods sequentially which build notification by getbroadcast . updateActiveNotification(downloads) And then updateCompletedNotification(downloads) . the first method checks my download list and builds an appropriate notification which is supposed to show the download progress(it builds the notifications every second and so it looks like ongoing notification!) , the second one checks for completed downloads and builds their notifications. Now the problem is that I set data through putExtra in these two methods for both on-going download and completed downloads, but when I get the intents in my receiver I can't get the putExtra data from the completed download intents and actually the data keys are from the first method .
I'm confused like hell! . these are two different methods which make different intents for different notifications . How is this possible.
Can anybody help me?!
Here is the example code :
first method :
Intent intent = new Intent(action);
Intent.setclass(receiver class)
intent.putExtra("someName1", boolean);
context.getbroadcast(intent);
second method :
Intent intent = new Intent(action);
Intent.setclass(receiver class)
intent.putExtra("someName2", boolean);
context.getbroadcast(intent);
now on reciever:
Bundle mybundle = intent.getExtras();
if(mybundle != null) {
for (String key : mybundle.keySet()) {
//Object value = mybundle.get(key);
Log.d("ALA-Dev", key);
}
}
for both intents (from the first method or the second one) it prints someName2 as the key ,meaning it dose's not get the second data at all!
Difficult to give a qualified answer without code examples. If you provided an extra like this:
Intent intent = new Intent(context, SomeActivity.class);
intent.putExtra("someName", someString);
context.startActivity(intent);
Then you can get the data back using soemthing like this:
Intent intent = getIntent();
String yourExtra = intent.getStringExtra("someName");
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 using a LocalActivityManager to have activities in different tabs, when I switch from a tab to another one, I start the Activity corresponding to the tab selected.
My problem is simple :
if I click on tab 1, I create intent11 and the first time, the method onCreate(Bundle emptyBundle) of Activity1 is called.
If I click on tab 2, I create intent2 and the method onCreate() is called.
Then, when I click on tab1, I create intent12 , the method onCreate(Bundle emptyBundle) is not called but onResume() is called (normal behavior).
I put special extras in the intent11 and intent12 to create Activity1, so I access it using getIntent().getExtras().
My problem is : the second time I go to the tab1, the intent12 is used to start the Activity, but the result of getIntent() is still intent11.
So I can't retreive the extras set in intent12, I can only retreive the extras set in intent11.
What am I doing wrong ? Should I avoid putting extras() in the intents ? Thank you.
Thank you.
PS : for the moment, I set a special flag to my intent to force to call onCreate(), but I'm sure it's not the good way of doing it.
I believe what you are looking for is here:
https://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29
onNewIntent(Intent newIntent) allows you to override the previous intent that was used to create/resume the app with the newest intent.
In Xamarin.Android / Monotouch I just added the following method to my Activity and it worked smoothly.
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
Intent = intent;
}
The principle should work fine also in Native Android.
no you should be able to still put the extras but I'm wondering if the extras are getting 'overwritten' when you are creating the new intents so I suggest trying this:
Put your extras into the bundle for the first intent you create, then before creating the next intent set your bundle to whatever might be in the bundle already by doing
Bundle bundle = getResultExtras(false);
Then you can create your new intent then when you are ready to get your data out of the bundle you can do
Bundle bundle = getResultExtras(false);
again and then get your data like you normally would from the bundle, just make sure that the extras your put in Intent1 don't have the same key name as the extras you put in Intent2
hope that helps some.
if you need more specific help it might be useful to post your code.
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.