TelephonyManager.listen的使用

本文深入探讨了TelephonyManager.java中的listen方法,解释了其作用、参数以及如何正确使用来监听手机call状态。同时,提供了一个简单的示例代码,帮助开发者更好地理解和应用此方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TelephonyManager.java中的listen 方法原型:

/**
     * Registers a listener object to receive notification of changes
     * in specified telephony states.
     * <p>
     * To register a listener, pass a {@link PhoneStateListener}
     * and specify at least one telephony state of interest in
     * the events argument.
     *
     * At registration, and when a specified telephony state
     * changes, the telephony manager invokes the appropriate
     * callback method on the listener object and passes the
     * current (udpated) values.
     * <p>
     * To unregister a listener, pass the listener object and set the
     * events argument to
     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
     *
     * @param listener The {@link PhoneStateListener} object to register
     *                 (or unregister)
     * @param events The telephony state(s) of interest to the listener,
     *               as a bitwise-OR combination of {@link PhoneStateListener}
     *               LISTEN_ flags.
     */
    public void listen(PhoneStateListener listener, int events) {
        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
        try {
            Boolean notifyNow = true;
            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
        } catch (RemoteException ex) {
            // system process dead
        } catch (NullPointerException ex) {
            // system process dead
        }
    }

private static ITelephonyRegistry sRegistry;

主要就是用来监听手机call状态,需要调用

mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);

来去除监听,否则会泄漏整个activity对象。

监听示例:

private TelephonyManager mTelephonyManager;
    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {

		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			super.onCallStateChanged(state, incomingNumber);
			if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
				//通话中
			} else if (state == TelephonyManager.CALL_STATE_IDLE) {}
		}
    	
    };


getCallStatus() { let that = this; let maintest = plus.android.runtimeMainActivity(); let Contexttest = plus.android.importClass("android.content.Context"); let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager"); let telManager = maintest.getSystemService(Contexttest.TELEPHONY_SERVICE); let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', { onReceive: function(Contexttest, intent) { plus.android.importClass(intent); // 关键修复:区分来电(RINGING)与接通(OFFHOOK) let state = intent.getStringExtra(telephonyManager.EXTRA_STATE); let incomingNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER); // 获取来电号码 // 调试日志输出完整参数 console.log("STATE:", state, "Number:", incomingNumber || "Outgoing Call"); console.log(222,telephonyManager.EXTRA_STATE_OFFHOOK); // 精确判断何时开始计时 if (state === telephonyManager.EXTRA_STATE_OFFHOOK) { // 仅当来电处于 RINGING 后的 OFFHOOK 或主动拨号接通时才计时 if (that.lastState === telephonyManager.EXTRA_STATE_RINGING || that.isOutgoing) { if (!that.timer) { console.log("通话实际接通,开始计时"); that.callStart = Date.now(); that.timer = setInterval(() => { that.duration = Math.floor((Date.now() - that.callStart)/1000); }, 1000); } } } else if (state === telephonyManager.EXTRA_STATE_RINGING) { that.lastState = state; // 记录来电振铃状态 that.isOutgoing = false; // 标记为来电 } else if (state === telephonyManager.EXTRA_STATE_IDLE) { if (that.timer) { clearInterval(that.timer); that.timer = null; that.duration = 0; that.isOutgoing = false; that.lastState = null; } } } }); let IntentFilter = plus.android.importClass('android.content.IntentFilter'); let filter = new IntentFilter(); filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED); maintest.registerReceiver(receiver, filter); },如何在这个代码中实现电话接通后再开始计时
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值