我在The method getSystemService(String) is undefined for the type WebAppInterface
使用“getSystemService”时遇到了这个错误。我在接口类工作,而不是在活动类工作。
@JavascriptInterface
public void Viber(String value ) {
if (value.equals("on")) {
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator)getSystemService(mContext.VIBRATOR_SERVICE);
// Vibrate for 300 milliseconds
v.vibrate(300);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是因为方法 getSystemService 属于类 Context,因此您必须在上下文中运行它,但您是从活动中运行它。
@JavascriptInterface
public void Viber(Context cn, String value) {
if (value.equals("on")) {
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) cn.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 300 milliseconds
v.vibrate(300);
}
}
Run Code Online (Sandbox Code Playgroud)