您的位置:首页 > 移动开发 > Android开发

Android弹出拨号界面和拨打电话实现

2012-10-12 21:05 309 查看
需要使用反射机制将ITelephony反射出来进行操作。

private void dial(String number) {

Class<TelephonyManager> c = TelephonyManager.class;

Method getITelephonyMethod = null;

try {

getITelephonyMethod = c.getDeclaredMethod("getITelephony",

(Class[]) null);

getITelephonyMethod.setAccessible(true);

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

Object iTelephony;

iTelephony = (Object) getITelephonyMethod.invoke(tManager,(Object[]) null);

Method dial = iTelephony.getClass().getDeclaredMethod("dial", String.class);

dial.invoke(iTelephony, number);

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



private void call(String number) {

Class<TelephonyManager> c = TelephonyManager.class;

Method getITelephonyMethod = null;

try {

getITelephonyMethod = c.getDeclaredMethod("getITelephony",

(Class[]) null);

getITelephonyMethod.setAccessible(true);

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

Object iTelephony;

iTelephony = (Object) getITelephonyMethod.invoke(tManager,(Object[]) null);

Method dial = iTelephony.getClass().getDeclaredMethod("call", String.class);

dial.invoke(iTelephony, number);

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: