【问题标题】:Twice Ringing state indication and no idle/offhook state indication in androidandroid 中的两次振铃状态指示和无空闲/摘机状态指示
【发布时间】:2018-07-23 07:21:17
【问题描述】:

当我尝试监控手机状态时,只经过振铃状态。每当来电被应答、拒绝或超时时,永远不会看到其他两种状态,空闲和摘机。并且有一个来电指示两个振铃。这太奇怪了。列出一个电话号码为 02xxxxxxxxx 的来电的运行日志,例如:


D/INCOMING:02xxxxxxxxx

D/ori铃声:1

D/州:响铃

D/AudioManager: setStreamVolume(streamType:2, index:7, flags:6)

D/INCOMING:02xxxxxxxxx

D/ori铃声:2

D/州:响铃

D/AudioManager: setStreamVolume(streamType:2, index:7, flags:6)

我添加权限并在 BroadcastReceiver 中注册手机状态更改。

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

    <receiver android:name=".PhoneCallHandler">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

我想知道为什么有两个振铃指示以及如何获得空闲和摘机状态的信息。谢谢。

信息列表代码:

public class PhoneCallHandler extends BroadcastReceiver {
static boolean flag = false;
@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try {
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        Bundle bundle = intent.getExtras();
        String phoneNumber = bundle.getString("incoming_number");
        Log.d("INCOMING", phoneNumber);

        String state = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        //Log.d("ACTION", action);

        AudioManager auManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int oriRingtone = auManager.getRingerMode();
        Log.d("oriRingtone", String.valueOf(oriRingtone));
        Log.d("State", state);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            switch (phoneNumber) {
                case "02584929790":
                    auManager.setStreamVolume(AudioManager.STREAM_RING, 7, AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);
                    break;
            }
        }else{
            auManager.setRingerMode(oriRingtone);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

【问题讨论】:

    标签: android


    【解决方案1】:

    广播接收器

    `public class IncommingCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
    
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    
            int state = 0;
            if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                state = TelephonyManager.CALL_STATE_IDLE;
    
    
            } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                state = TelephonyManager.CALL_STATE_OFFHOOK;
    
    
    
            } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                state = TelephonyManager.CALL_STATE_RINGING;
    
    
            }
    
        }
    }`
    

    清单

    <receiver android:name=".IncommingCallReceiver">
            <intent-filter android:priority="999">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    

    权限

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

    【讨论】:

    • 谢谢,Siddhesh。我的代码和你类似。但是我没有看到 IDLE 和 OFFHOOK 状态。你知道如何获得这些状态吗?
    • 如果未接电话则->振铃->空闲,如果接听电话则通话->振铃->摘机->空闲,如果挂断电话则->振铃->空闲。
    • 但我从未在日志和代码执行结果中看到这些状态事务。你知不知道怎么?我列出了我的代码以供您参考。
    • 用于测试您正在使用的设备?
    • 我用的是HTC D826w
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2016-04-06
    • 2020-09-30
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2020-12-22
    相关资源
    最近更新 更多