Android 调用振动触发器.io模块

Android 调用振动触发器.io模块,android,trigger.io,Android,Trigger.io,我正在尝试为trigger.io模块创建一个振动函数(android/java新增) 我的代码在下面,但我一直都在得到这个 类型API的getSystemService(String)方法未定义 我不是在导入库或其他东西吗 public class API { public static void pulse(final ForgeTask task){ Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_S

我正在尝试为trigger.io模块创建一个振动函数(android/java新增)

我的代码在下面,但我一直都在得到这个

类型API的getSystemService(String)方法未定义

我不是在导入库或其他东西吗

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

我假设您希望调用getSystemService(),就像从活动中调用一样

您需要传递上下文来完成此操作(可以是您的活动),并在上下文中调用它

例如,您可以在构造函数中传递它:

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

可以通过调用
ForgeApp.getActivity()

这将给你:

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

啊,我想我遗漏了一些东西,我现在只需要检查它是否工作。它不工作,它不断地给出
“类型”:“意外的\u故障”
NullPointerException
这只是一个通用的本机Android解决方案,我猜它与trigger.io不工作:)从trigger.io得到了一个相同响应的回复,但感谢这修复了它!