在构造函数里实例化context
this.targetContext=InstrumentationRegistry.getTargetContext();
public void hangUpCommand() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { TelephonyManager telephonyManager = (TelephonyManager)targetContext.getSystemService(Context.TELEPHONY_SERVICE); Class<TelephonyManager> c = TelephonyManager.class; Method mthEndCall = c.getDeclaredMethod("getITelephony",(Class[])null); mthEndCall.setAccessible(true); final Object obj = mthEndCall.invoke(telephonyManager,(Object[])null); Method mt = obj.getClass().getMethod("endCall"); mt.setAccessible(true); mt.invoke(obj); }