JAVA反射数组时注意的事项

阅读 171

2022-09-27


今天在给时钟增加来电仅震动三下的需求时,需要通过调动反射来处理。

传普通的int、long、String参数可以百度传,

但在调用响三下的时候遇到了麻烦,如何通过反射传一个long[]?


解决方法

Vibrator mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
String effectName = "STRONG_DOUBLE_TAP";
long [] pattern = {200,800,200,800,200,800};
int repeat = -1;
Method method = null;
try {
method=mVibrator.getClass().getMethod("amigoVibrate", new Class[]{String.class, long[].class, int.class}); //注意这两行
method.invoke(mVibrator, new Object[]{effectName, pattern, repeat});
Log.e("***","闹钟方法调用了");
} catch (Exception e) {
Log.e(TAG, "failed caurse : " + e.toString());
}


在调用反射的时候会报Exception(仔细看Log),在AndroidManifast.xml中注册相关权限。


精彩评论(0)

0 0 举报