2

更新:

“请参阅下面的评论了解最新状态。”

我正在尝试在我的应用程序中使用此类“com.android.internal.CallManager”。我研究这个话题好几天了。但是,除了thisthis ,我什么也没找到。他们都不能解决我的问题。我使用了两种方法来调用 Android 的隐藏 API。

1) 我用来调用隐藏 API 的第一种方法是 Java 反射。这是我的示例代码:

ClassLoader classLoader = context.getClass().getClassLoader();
final Class<?> callManagerClass = classLoader.loadClass("com.android.internal.telephony.CallManager");

Method getInstanceMethod = callManagerClass.getDeclaredMethod("getInstance");
getInstanceMethod.setAccessible(true);
mCallManager = getInstanceMethod.invoke(null);

mActiveFgMethod = callManagerClass.getDeclaredMethod("hasActiveFgCall");
Object res = mActiveFgMethod.invoke(mCallManager);

boolean result = ((Boolean) res).booleanValue();

如您所见,我使用 Java Reflection 访问 CallManager 并访问函数“hasActiveFgCall”。但无论电话状态是什么,结果都是false

2)我使用的第二种方式就像这个网站(http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction /)使用的。以下是示例代码。

import com.android.internal.telephoney.CallManager;

CallManager mCallManager = CallManager.getInstance();
boolean result = mCallManager.hasActiveFgCall();

我下载并编译了 Android 4.1.2 源代码。jar 文件是这个“out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar”。我可以成功调用类和函数。不幸的是,结果是一样的,false

以下是我添加的权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.USE_SIP" />

我检查了这个类文件(CallManager.java)的代码,找到了函数调用列表:hasActiveFgCall() --> getFirstActiveCall() --> isIdle()。我以为我误解了手机状态,所以我写了一个应用程序来验证手机状态。代码如下:

public class MainActivity extends Activity {
    private static final String TAG = "Telephony";

    TextView t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1 = (TextView) this.findViewById(R.id.text1);

        TelephonyManager mTelephonyMgr = (TelephonyManager) 
                this.getSystemService(Context.TELEPHONY_SERVICE);

        mTelephonyMgr.listen(new TeleListener(), 
                PhoneStateListener.LISTEN_CALL_STATE);

        t1.setText("listen the state of phone:\n"); 
    }

    class TeleListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.e(TAG, "CALL_STATE_IDEL");
                t1.append("CALL_STATE_IDLE"+"\n");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.e(TAG, "CALL_STATE_OFFHOOK");
                t1.append("CALL_STATE_OFFHOOK" + "\n");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.e(TAG, "CALL_STATE_RINGING");
                t1.append("CALL_STATE_RINGING"+"\n");
                break;

            default:
                break;

            }
        }
    }
}

然而,我是对的。无论电话状态是什么,例如摘机、空闲和振铃,函数“hasActivieFgCall()”都会返回false。这非常令人沮丧。

任何答复将不胜感激。

4

1 回答 1

0

It doesn't like the Service like ITelephony which can be used by Java Reflection and aidl. I think the only way to use CallManager is to give your app the system permission

you need to signapk your apk and add android:sharedUserId="android.uid.system" in your mainfest file.

于 2014-03-05T07:55:38.840 回答