문제

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);
        }
    }
}
도움이 되었습니까?

해결책

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);

다른 팁

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);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top