欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android监听通话状态

程序员文章站 2024-01-16 20:27:52
...

samsung提审结果报告里:

application sound is audible during the call in pop up window
//在弹出窗口中的呼叫期间,<缺陷>应用程序声音可听见

解决方案:监听通话状态管理游戏音效(方法一适用于通话界面监听,方法二通用)
方法一:

1>在activity创建时创建通话状态监听
	//1.获取telephone的实例
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    //2.注册电话的监听
    tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);

2>监听响应
	public class MyPhoneStateListener extends PhoneStateListener {
        //当电话设置状态发生改变的时候调用
        public void onCallStateChanged(int state, String phoneNumber) {
            System.out.println("state==>"+state);
            System.out.println("phoneNumber==>"+phoneNumber);
            //具体判断下电话得状态
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    //System.out.println("停止");
                	//打开声音
                	javaCallVs.runGoOpenVoice();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK://接听状态
                    //System.out.println("开始");
                	//关闭声音
                	javaCallVs.runGoCloseVoice();
                    break;
                case TelephonyManager.CALL_STATE_RINGING://电话响铃状态
                    //System.out.println("准备");
                	javaCallVs.runGoCloseVoice();
                    break;
                default:
                	javaCallVs.runGoCloseVoice();
                    break;
            }
        }
    }

但是上述方法在samsung手机的弹出窗口中没有监听到
就是这种情况:当游戏正在运行时,有电话接听或者拨打电话时不使用通话界面而是选择挂起或者悬浮窗通话,而在通话界面里是可以监听到的,暂时不知道原因
方法二:

1>在Manifest注册一个新类,监听系统通话状态广播
		<receiver android:name=".PhoneReceiver">
			<intent-filter android:priority="1000">
				<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
				<action android:name="android.intent.action.PHONE_STATE"/> 
			</intent-filter>
		</receiver>
		
2>在Manifest添加权限
	<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
	<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
	
3>实现类PhoneReceiver 
	package com.ilink.xxx.nearme.gamecenter;
	import android.app.Service; 
	import android.content.BroadcastReceiver; 
	import android.content.Context; 
	import android.content.Intent; 
	import android.telephony.PhoneStateListener; 
	import android.telephony.TelephonyManager;
	import android.util.Log;
	
	import com.leduo.talk.javaCallVs;
	
	public class PhoneReceiver extends BroadcastReceiver { 
		private static final String TAG = "message";
	    private static boolean mIncomingFlag = false;
	    private static String mIncomingNumber = null;
	
	    @Override
	    public void onReceive(Context context, Intent intent) {
	        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
	        	// 如果是拨打电话
	            mIncomingFlag = false;
	            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
	            Log.i(TAG, "call OUT:" + phoneNumber);
	            
	        } else {
	            // 如果是来电
	            TelephonyManager tManager = (TelephonyManager) context
	                    .getSystemService(Service.TELEPHONY_SERVICE);
	            switch (tManager.getCallState()) {
	            
	            case TelephonyManager.CALL_STATE_RINGING:
	                mIncomingNumber = intent.getStringExtra("incoming_number");
	                Log.i(TAG, "RINGING :" + mIncomingNumber);
	                Log.d(TAG, "**********************CALL_STATE_IDLE!!!!**********************");
	                javaCallVs.runGoOpenVoice();
	                break;
	            case TelephonyManager.CALL_STATE_OFFHOOK:
	                if (mIncomingFlag) {
	                    Log.i(TAG, "incoming ACCEPT :" + mIncomingNumber);
	                }
	                Log.d(TAG, "**********************CALL_STATE_OFFHOOK!!!!**********************");
	                javaCallVs.runGoCloseVoice();
	                break;
	            case TelephonyManager.CALL_STATE_IDLE:
	                if (mIncomingFlag) {
	                    Log.i(TAG, "incoming IDLE");
	                }
	                System.out.println("挂断");
					Log.d(TAG, "**********************CALL_STATE_IDLE!!!!**********************");
					javaCallVs.runGoOpenVoice();
	                break;
	            }
	        }
	    }
	} 
相关标签: 通话状态监听