【问题标题】:Call vibration trigger.io module调用震动 trigger.io 模块
【发布时间】:2014-01-30 11:28:55
【问题描述】:

我正在尝试为 trigger.io 模块创建振动功能(android/java 的新功能)

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

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

我不是在导入库还是什么?

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

【问题讨论】:

    标签: android trigger.io


    【解决方案1】:

    可以通过调用 ForgeApp.getActivity() 来获取 Trigger.io 模块中的上下文

    这会给你:

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

    【讨论】:

    • 收到 trigger.io 的回复,回复相同,但感谢修复!
    【解决方案2】:

    我假设您想像从活动中一样调用 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);
            }
        }
    }
    

    【讨论】:

    • 啊,我以为我遗漏了什么,我只需要检查它现在是否有效。
    • 它不起作用它一直给"type":"UNEXPECTED_FAILURE"NullPointerException
    • 这只是一个通用的原生Android解决方案,猜想它不适用于trigger.io :)
    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2013-11-30
    相关资源
    最近更新 更多