user77115
user77115

Reputation: 5577

How do I get the Context from the Intent?

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.

Upvotes: 32

Views: 32232

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

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.

Upvotes: 34

Related Questions