2011-12-05 35 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);