1

I'm trying to access the vibrator using the following in class called VibrationManager and the class is not extending Activity

Vibrator v =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

but eclipse throws and error like

The method getSystemService(String) is undefined for the type VibrationManager

This is my whole Class

public class VibrationManager {

private static VibrationManager me = null;

Vibrator v = null;

private Vibrator getVibrator(){
    if(v == null){
        v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }
    return v;
}

public static VibrationManager getManager() {
    if(me == null){
        me = new VibrationManager();
    }
    return me;
}

public void vibrate(long[] pattern){

}

}

Please help

Beast
  • 617
  • 2
  • 8
  • 19

2 Answers2

3

Your class doesn't have the method getSystemService since this class dosen't extend a Activity.

If you wan't to use the getSystemService method you need your class VibrationManager to extend an activity or you need to receive a context for that.

Just change your code to use a context, for that you will need to also get a context in your static call.

public class VibrationManager {

    private static VibrationManager me;
    private Context context;

    Vibrator v = null;

    private Vibrator getVibrator(){
        if(v == null){
            v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        }
        return v;
    }

    public static VibrationManager getManager(Context context) {
        if(me == null){
            me = new VibrationManager();
        }
        me.setContext(context);
        return me;
    }

    private void setContext(Context context){
        this.context = context;
    }

    public void vibrate(long[] pattern){

    }
}
Mykola
  • 3,343
  • 6
  • 23
  • 39
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • sorry but look at your code and you didn't copy the right code, try to understand what I have done here just trying to copy my code without understand the problem isn't the best solution for you – GhostDerfel Mar 14 '14 at 19:07
  • Thanks mate. So whenever we want to use the systemService we need to pass the context variable ? or can't we create one and do ? – Beast Mar 14 '14 at 19:09
  • @Beast I just posted your hole class with some changes so you can copy the code, and yes you need the context to access this method so you can pass this from any Activity – GhostDerfel Mar 14 '14 at 19:11
  • Slight note about this code: if you pass the activty as a context, it will retain it in memory even if the activity goes to background and gets destroyed by the system (due to the indirect static reference). – mrlem Apr 20 '18 at 08:51
2

If you have problems accessing the context from different Views you can do this:

  • Create a class that extends Application (e.g. MyApplication)

  • Declare that class in your manifest as your Application class as shown below:

     <application
       android:name="your.project.package.MyApplication"
       ...
    
  • Application class is by default a singleton, but you need to create a getInstance method as shown below:

    public class MyApplication extends Application {
    
        private static MyApplication instance;
    
        public void onCreate() {
            instance = this;
        }
    
        public static MyApplication getInstance() {
            return instance;
        }
    }
    

Done, you can access the context from anywhere in your app without passing so many references as follows:

MyApplication app = MyApplication.getInstance()
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);

There you go, you can not only call vibrator service but any service you want...

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • 2
    If you chose to go using a singletone Aplication I recomend you reed this post: http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android/3827166#3827166 – GhostDerfel Mar 14 '14 at 19:14
  • 1
    Yeah, I just recomended that because of the explenation and also because in this post I think people start a good discussion about when and why you need to use the singletone pattern for the application, but yes your answer is also correct and is also very simple :) – GhostDerfel Mar 14 '14 at 19:20
  • @MartinCazares can you please tell how to add the class as application class in manifest, i'm very noob to android – Beast Mar 14 '14 at 19:44
  • @MartinCazares will this method work if i call from the remoteService class ? – Beast Mar 14 '14 at 19:53