How to provide context for calling required functions in my own class

Sometimes I need to provide a Context object to call certain functions, such as

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

to start the service

context.startService(intent);

Or, provide a Context object to execute the request

Cursor cursor = context.managedQuery(uri, projection, null, null, null);

If this is done in a UI class that extends Activity, that’s fine. However, if I want to create my own utility class (singleton), which does not extend anything and calls these functions, I do not have the necessary Context object. Now, my workaround is to pass a link to the activity when initializing the utility class and its link to calling these functions. I am wondering how to do it right. It should not be reasonable for each class to expand the context so that it can call these functions.

+2
2

Application onCreate -Method . Application -Object , getApplication. Application , .

, , .

+5

- . , .

. , .

Context . getApplicationContext() singleton . , .

+6

All Articles