编程语言
首页 > 编程语言> > Android 通过Java反射 拨打、挂断电话

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"/><!-- 拨打电话的权限 -->

 

标签:Java,getITelephonyMethod,挂断,Object,TelephonyManager,iTelephony,Android,null,Meth
来源: https://blog.51cto.com/u_15298588/3034519