How to use getSystemService in a non-activity class?

AndroidAndroid Context

Android Problem Overview


I am building an application which triggers an alarm via AlarmManager.

I would like to be able to call the Alarm via it's own non-activity class, but since I am not extending Activity, I don't appear to have any 'context'. This concept confuses me, and I've read the sdk docs.

How would I go about using:

alarmTest = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

in my non-activty class?

Also, I'm assuming getting context will allow me to use SharedPrefs and Intents in my non-activity class as well?

Android Solutions


Solution 1 - Android

You can pass the context to the non-activity class which is the preferred way or you could encapsulate the base context of the application to a singleton which would allow you to access the context anywhere within the application. At some cases this might be a good solution but in others its certainly not a good one.

Anyway, if you want to trigger an alarm via the AlarmManager I'm pretty sure the alarm should inherit from a Service or better yet from IntentService and in such cases you have access to the context via this.getBaseContext() or this.getApplicationContext()

Solution 2 - Android

Service and Activity inherit from Context - so when you are calling getSystemService in these classes, you are really calling super.getSystemService.

If you want to have a Context available in another class, you can pass one as an argument to a method of that class, keep a reference to it, etc.

Edit: Code sample. But seriously, it is extremely basic - if you understand inheritance and methods.

class MyActivity extends Activity { // Activity extends Context, so MyActivity also extends Context
  void someMethod() {
    MyOtherClass.useStaticContext(this);
    MyOtherClass instance = new MyOtherClass();
    instance.useInstanceContext(this.getApplicationContext());
  }
}
class MyOtherClass {
  static void useStaticContext(Context context) {
  }
  void useInstanceContext(Context context) {
  }
}

Solution 3 - Android

You can try the following, it allows you to get the current context the view is running through.

alarmTest = (AlarmManager)this.getContext().getSystemService(Context.ALARM_SERVICE);

Solution 4 - Android

You need to pass a context into the non-activity class.

Solution 5 - Android

Use this in Activity:

private Context context = this;

........
if(Utils.isInternetAvailable(context){
Utils.showToast(context, "toast");
}
..........

in Utils:

public class Utils {

    public static boolean isInternetAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
    }

    public static void showToast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneportermdView Question on Stackoverflow
Solution 1 - AndroidMarek SzanyiView Answer on Stackoverflow
Solution 2 - AndroidJean HominalView Answer on Stackoverflow
Solution 3 - AndroidKevin ParkerView Answer on Stackoverflow
Solution 4 - AndroidRyan ReevesView Answer on Stackoverflow
Solution 5 - AndroiddimvolkView Answer on Stackoverflow