首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >呼叫振动trigger.io模块

呼叫振动trigger.io模块
EN

Stack Overflow用户
提问于 2014-01-30 11:28:55
回答 2查看 127关注 0票数 0

我试图为trigger.io模块创建一个振动功能(对android/java来说是新的)。

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

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

我不是在进口图书馆什么的吗?

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-30 13:19:00

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

这会让你:

代码语言:javascript
复制
Vibrator v = (Vibrator)ForgeApp.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
票数 1
EN

Stack Overflow用户

发布于 2014-01-30 11:38:24

我假设您想像调用活动中的getSystemService()一样调用()?

您需要传递上下文(可以是您的活动)并在上下文中调用它。

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

代码语言:javascript
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21455216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档