Android 通过Java反射 拨打、挂断电话

public void call(String number)
{
    Class<TelephonyManager> c = TelephonyManager.class;
    Method getITelephonyMethod = null;
    try {
        getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
        getITelephonyMethod.setAccessible(true);
        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Object iTelephony = (Object) getITelephonyMethod.invoke(tManager,(Object[]) null);
        Method call = iTelephony.getClass().getDeclaredMethod("call", String.class);
        call.invoke(iTelephony, number);
    }catch (Exception e){
        e.printStackTrace();
    }
}

public void endCall()
{
    Class<TelephonyManager> c = TelephonyManager.class;
    Method getITelephonyMethod = null;
    try {
        getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
        getITelephonyMethod.setAccessible(true);
        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Object iTelephony = (Object) getITelephonyMethod.invoke(tManager,(Object[]) null);
        Method endCall = iTelephony.getClass().getDeclaredMethod("endCall");
        endCall.invoke(iTelephony);
    }catch (Exception e){
        e.printStackTrace();
    }
}
<uses-permission android:name="android.permission.CALL_PHONE"/><!-- 拨打电话的权限 -->

 

上一篇:【模板】树的直径


下一篇:EM期望最大化算法