What is the difference betweeen Intent(getApplicationContext(), class) and Intent(class) - android

I'm working on a code as a reference and encountered this Intent call: Intent(getApplicationContext(), secondActivity.class)
I got baffled because in my projects I only use: Intent(".mYSecondActivity")
I tried removing getApplicationContext() and changed it to Intent(".secondActivity")' in which the name is registered in the manifest. I run the application and Forced Closed. What is the significance of getApplicationContext()?
secondActivity.class is coded to retrieve data from a PHP MYSQL database.

"This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you."
source: http://developer.android.com/reference/android/content/Intent.html#Intent%28android.content.Context,%20java.lang.Class%3C?%3E%29

Related

explanation about intent.getStringExtra and intent.putExtra in android studio

I kinda new about using android studio and my teacher asked me to learn about how to use intent.getStringExtra and intent.putExtra.
please help to explain me about those two things.
thanks!
In general, intents are used to move between some android component like (activity, service, broadcast receivers ...etc) some time you need to pass some value between these components so you need to use put extra in the sender component and get extra in the receiver for example :
in the sender :
Intent intent = new Intent(SenderActrivtiy.this, REciverActivity.class);
intent.putExtra("emailKey", "mm#email.com");
startActivity(intent);
in the reciver:
String email =getIntent().getStringExtra("emailKey");
note that you need to pass the same key to retrieve your value

In Android Intent why we write ActivityTwo.class in second argument?

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
Why ActivityTwo.class??
Why not .java please explain
Thank you
Second parameter in the Intent call is a component. We are actually setting a component name as the second parameter which is a class name. You can read about component here in this link. https://developer.android.com/reference/android/content/ComponentName.html
This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information.

Which function does intent have? [duplicate]

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

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.

android IntentService what can be wrong when intent is empty?

hi
After creating intents for month now i suddenly
hit the wall when in my Notification PendingIntent, i did this:
Intent intent = new Intent(getApplicationContext(), SendFileService.class);
intent.putExtra("uuid", "123-456-34");
The SendFileService is an IntentService.
To my surprise in the IntentService onHandleIntent the extras.getString("uuid");
was null.
what can possible be the reason .
I even added an action to test but still the same.
Im clearly did something wrong or missing some knowledge about this.
Any ide?
From the docs
The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Categories

Resources