我试图为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);
}
}
}发布于 2014-01-30 13:19:00
在Trigger.io模块中获取上下文可以通过调用ForgeApp.getActivity()来完成
这会让你:
Vibrator v = (Vibrator)ForgeApp.getActivity().getSystemService(Context.VIBRATOR_SERVICE);发布于 2014-01-30 11:38:24
我假设您想像调用活动中的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);
}
}
}https://stackoverflow.com/questions/21455216
复制相似问题