2

我需要访问 com.android.internal.telephony.Call。

这样做:

// Initialize the telephony framework
PhoneFactory.makeDefaultPhones (this);

// Get the default phone
Phone phone = PhoneFactory.getDefaultPhone ();

CallManager mCM = CallManager.getInstance ();
mCM.registerPhone (phone);

Call call = mCM.getFirstActiveBgCall();

但不扩展以初始化框架。

帮我初始化呼叫。

我需要读取呼叫的状态,例如:IDLE、ACTIVE、HOLDING、DIALING、ALERTING、INCOMING、WAITING、DISCONNECTED、DISCONNECTING。

4

2 回答 2

0

您需要使用PhoneStateListener 它将为您提供让您的应用程序监听电话的不同状态的工具。您需要放入<uses-permission android:name="android.permission.READ_PHONE_STATE"/>清单文件

于 2012-06-10T09:19:16.077 回答
0

您可以,但有一个关键要求:应用程序必须在系统级别签名,这意味着您是制造商

这是您编写服务的方法,该服务将为前台调用状态的每次更改广播一个意图。

/*
 * This implementation uses the com.android.internal.telephony package: you have
 * to extract the framework classes .jar file from the platform (or the
 * emulator) to compile this code. Also, add the jar file to the external
 * libraries in the Java Build Path/libraries of the android project. </p>
 * 
 * The jar file must match the android version you are building the application
 * for. Because this implementation is using the internal packages it cannot be
 * guaranteed to operate on later versions of android.
 */
public class CallStateNotificationService extends Service {

    private static final String LOG_TAG = CallStateNotificationService.class.getSimpleName();

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            if (msg.what == 101) {
                CallManager callManager = CallManager.getInstance();
                Call.State state = callManager.getActiveFgCallState();

                Intent intent = new Intent(PhoneIntents.ACTION_PRECISE_CALL_STATE);
                intent.putExtra(PhoneIntents.PRECISE_CALL_STATE, state.name());

                Context context = getApplicationContext();
                context.sendBroadcast(intent);
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            CallManager callManager = CallManager.getInstance();
            if (callManager != null) {
                callManager.registerForPreciseCallStateChanged(mHandler, 101, null);
            } else {
                Log.w(LOG_TAG, "Can't resolve CallManager reference"); //$NON-NLS-1$
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        CallManager callManager = CallManager.getInstance();
        if (callManager != null) {
            callManager.unregisterForPreciseCallStateChanged(mHandler);
        } else {
            Log.w(LOG_TAG, "Can't resolve CallManager reference"); //$NON-NLS-1$
        }
    }

这是自定义广播意图的定义。

/** Intent action and extra argument names for CallStateNotificationService */
public final class PhoneIntents {

    public static final String ACTION_PRECISE_CALL_STATE = "com.myorg.myapp.CALL_STATE";

    public static final String PRECISE_CALL_STATE = "precise_call_state";
}

要编译和链接此代码,您当然需要将程序构建为 android 发行版本身的一部分,或者通过 Internet 上其他地方解释的方法导入类框架。

所有这些目前都在一个正在生产的应用程序中。

于 2013-05-15T08:15:36.867 回答