Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Context from the Intent?

Tags:

android

AccountAuthenticator.java:

Intent intent = new Intent(context, AccountActivity.class); 

AccountActivity.java:
In onCreate(Bundle aBundle) I want to say:

getIntent().getContext(); 

But getContext() does not exist.

How do I get the Context from the Intent ?
Since it's passed in the Intent constructor, I was expecting it to be available on arrival in the AccountActivity.

like image 649
user77115 Avatar asked Jul 12 '10 10:07

user77115


People also ask

What is context in intent?

Each object is given its own context, which contains the resources required to set that object up. It is required for many objects to be created, and to get program identifying information, among other purposes. This makes it invaluable to set up new views and activities, but it can also be used for other purposes.

What is intent and context in Android?

context. startActivity(new Intent(context, ActivityClass. class)); This call to startActivity sends the Intent to the Android system, which is then in charge of creating and opening the activity that you have specified.


1 Answers

How do I get the Context from the Intent ?

You don't.

Since it's passed in the Intent constructor, I was expecting it to be available on arrival in the AccountActivity.

The Context is only used to help create the Intent routing information. Since an Intent can (and frequently does) live outside of any Context, an Intent cannot hold onto a Context.

I need the Service (i.e. Context) that created the Intent, so as to be able to create an AsyncTask taking it in the constructor.

You cannot do this, sorry.

If I don't do that I get: "java.lang.SecurityException: caller uid 10027 is different than the authenticator's uid", since the AsyncTask is doing Accountmanager am = Accountmanager.get(context).

This has nothing to do with AsyncTask. This has to do with processes, not threads.

like image 139
CommonsWare Avatar answered Oct 13 '22 00:10

CommonsWare