Question

Im trying to create a vibration function for a trigger.io module (new to android/java)

The code I have is below, but i keep getting this

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

Am I not importing a library or something?

public class API {
    public static void pulse(final ForgeTask task){
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        // Output yes if can vibrate, no otherwise
        if (v.hasVibrator()) {
            v.vibrate(400);
        }
    }
}
Était-ce utile?

La solution

Getting hold of Context in a Trigger.io module can be done via a call to ForgeApp.getActivity()

Which would give you:

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

Autres conseils

I assume you want to call getSystemService() as you you would from an activity?

You will need to pass the context to do that (can be your activity) and call it on context

For example, you could pass it in the constructor:

public class API {

    Context mContext;

    public API(Context context) {
        mContext = context;
    }

    public static void pulse(final ForgeTask task){
        Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);

        // Output yes if can vibrate, no otherwise
        if (v.hasVibrator()) {
            v.vibrate(400);
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top