gpt4 book ai didi

java - 如何知道我是否在 Android 上通话?

转载 作者:IT老高 更新时间:2023-10-28 21:04:34 28 4
gpt4 key购买 nike

我想知道我是否在通话中。

如果我正在通话,请启动服务(服务部分很清楚)。我该怎么做?

在接听电话时,我需要调用服务电话...我不知道该怎么做?有什么帮助吗?

最佳答案

你需要广播接收器...

在 list 中声明广播接收器...

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

同时声明使用权限...

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

广播接收器类 ...

package x.y;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

}

}

还有一个自定义电话状态监听器的类...

package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CustomPhoneStateListener extends PhoneStateListener {

//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);

switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}

关于java - 如何知道我是否在 Android 上通话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948961/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com