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

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


發佈了45 篇原創文章 · 獲贊 8 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章