简体   繁体   English

TelephonyManager getstate()在android中返回零

[英]TelephonyManager getstate() returns zero in android

In my application, I want to put a log for the state when someone picks up the call. 在我的应用程序中,我想为有人接听电话时的状态记录日志。 The getState() on the switch statement must return the correct state of the call, but it always returns zero. switch语句上的getState()必须返回正确的调用状态,但始终返回零。 Here is my onRecieve() method: 这是我的onRecieve()方法:

  public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
        incomingFlag = false;
        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.i(TAG, "call OUT:"+phoneNumber);
        TelephonyManager tm =
                (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
        Log.e("log state", String.valueOf(tm.getCallState()));
        switch (tm.getCallState()) {
            case TelephonyManager.CALL_STATE_RINGING:
                incomingFlag = true;
                incoming_number = intent.getStringExtra("incoming_number");
                Log.i(TAG, "RINGING :"+ incoming_number);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(incomingFlag){
                    Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if(incomingFlag){
                    Log.i(TAG, "incoming IDLE");
                }
                break;
                default:
                    Log.e("ds","Error");
        }
    }

manifest file : 清单文件:

     <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".BroadCast" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        </intent-filter>
    </receiver>

Permissions : 权限:

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name = "android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

The onCreate() method mentioned above is in a seperate class I created called BroadCast, and I call it by creating a new instance of it. 上面提到的onCreate()方法位于一个单独的类中,该类名为BroadCast,我通过创建它的新实例进行调用。

Please let me know if more details are required. 如果需要更多详细信息,请告诉我。

Try to use a BroadcastReceiver to handle the incoming call. 尝试使用BroadcastReceiver处理来电。 In your onResume, set up the receiver 在您的onResume中,设置接收器

IntentFilter filter2 = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter2.setPriority(99999);
this.registerReceiver(incomingCallReceiver, filter2);

and handle it such as 并处理如

BroadcastReceiver incomingCallReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        if (bundle == null) return;
        // Incoming call

        // Get the state
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);

        // Process the states
        if ((state != null) &&  (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
            // Ringing State
        }
        if ((state != null) &&  (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))) {
            // Idle State
        }
        if ((state != null) &&  (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
            // Offhook State
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM