1. 程式人生 > >Android彈出撥號介面和撥打電話實現

Android彈出撥號介面和撥打電話實現

privatevoid 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();
        }
    }
    
    
privatevoid 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();
        }
    }