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

来源:互联网 发布:安卓翻墙软件 编辑:程序博客网 时间:2024/05/14 10:05

http://www.blogjava.net/TiGERTiAN/archive/2011/01/25/343499.html

 

需要使用反射机制将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();
        }
    }


原创粉丝点击