如何在Android中获取Phone实例?

use*_*299 3 android telephony

我必须在android电话上使用android ITelephony内部类.我正在使用ITelephony通过获取其实例来进行调用

ITelephony phone = ITelephony.Stub.asInterface(
    ServiceManager.getService(Context.TELEPHONY_SERVICE)
);
Run Code Online (Sandbox Code Playgroud)

然后打电话给

phone.call(destNum);
Run Code Online (Sandbox Code Playgroud)

现在我需要执行其他操作,例如保持呼叫.ITelephony没有为此提供了一个API,但是我发现有一个电话类switchHoldingAndActive(),但调用这个我需要一个电话实例的当前运行的通话.我试过了

Phone PhoneActive = PhoneFactory.getDefaultPhone();
Run Code Online (Sandbox Code Playgroud)

但它给了我一个例外说

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

获取电话实例的正确方法是什么?

ale*_*rik 5

没有正确的方法来获取Phone实例.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);
Run Code Online (Sandbox Code Playgroud)