2011-12-05 83 views
3

我必須使用android ITelephony android電話上的內部類。我使用ITelephony通過獲取其實例作爲如何在android中獲取Phone實例?

ITelephony phone = ITelephony.Stub.asInterface(
    ServiceManager.getService(Context.TELEPHONY_SERVICE) 
); 

撥打電話,然後用

phone.call(destNum); 

現在我需要執行像抱着一個呼叫其他行動呼籲。 ITelephony沒有爲此提供API,但是我找到了一個具有switchHoldingAndActive()的Phone類,但爲了調用此類,我需要一個Phone實例到當前正在運行的活動呼叫。我試圖

Phone PhoneActive = PhoneFactory.getDefaultPhone(); 

但它給了我一個異常說

Caused by: java.lang.RuntimeException: 
    Can't create handler inside thread that has not called Looper.prepare() 

什麼是得到一個電話實例的正確方法?

回答

-1
Can't create handler inside thread that has not called Looper.prepare() 

是,當你調用一個Thread一個UI線程僅法,是不是個UI線程拋出的異常。

嘗試要麼使用AsyncTask代替Thread,叫runOnUiThread,或postRunnable

+0

喜是我的是不是一個UI線程它的服務,但我使用ASYN任務請看我的代碼,讓我知道我應該更改 – user954299

+0

in onExExcute方法的ASYNC任務,我打電話Phone.getDefaultPhone() – user954299

5

沒有正確的方法來獲得電話實例。 AFAIK,getDefaultPhone()僅當您的線程(通過PhoneFactory.makeDefaultPhone())創建它時纔會返回當前活動Phone子類(GSM/CDMA)的實例。 不過,我相信這個實例是通過預先安裝的Call應用程序創建的,所以如果你嘗試它將無法工作。 很久以前,我嘗試使用反射來註冊精確的呼叫狀態更新並因安全異常而卡住。我的應用程序必須在系統進程中運行... 我的代碼是這樣的:

mPhoneFactory = Class.forName("com.android.internal.telephony.PhoneFactory"); 
Method mMakeDefaultPhone = mPhoneFactory.getMethod("makeDefaultPhone", new Class[] {Context.class}); 
mMakeDefaultPhone.invoke(null, getContext()); 

Method mGetDefaultPhone = mPhoneFactory.getMethod("getDefaultPhone", null); 
mPhone = mGetDefaultPhone.invoke(null); 

Method mRegisterForStateChange = mPhone.getClass().getMethod("registerForPreciseCallStateChanged", 
new Class[]{Handler.class, Integer.TYPE, Object.class});    

mRegisterForStateChange.invoke(mPhone, mHandler, CALL_STATE_CHANGED, null);