Java 如何知道我是否在安卓上打电话?
我想知道我是否在打电话 如果我在通话中,则启动服务(服务部分已清除)。我该怎么做Java 如何知道我是否在安卓上打电话?,java,android,telephonymanager,Java,Android,Telephonymanager,我想知道我是否在打电话 如果我在通话中,则启动服务(服务部分已清除)。我该怎么做 在接听电话时,我需要拨打服务电话。。。我不知道该怎么做?有什么帮助吗?您需要广播接收器 <receiver android:name=".PhoneStateBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE
在接听电话时,我需要拨打服务电话。。。我不知道该怎么做?有什么帮助吗?您需要广播接收器
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
在清单中声明广播接收器
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
和一个自定义电话状态侦听器的类
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;
}
}
}
您只能知道电话即将打来,但无法修改此选项:( 看到这个了吗 返回其中一个
CALL\u STATE\u IDLE
CALL\u STATE\u OFFHOOK
CALL\u STATE\u RINGING
如果这符合您的要求,那么它的代码要比Pied Piper更全面的解决方案少得多。毫无疑问,这是一个老问题。但它是Google上的第一个结果。因此,我决定添加另一个有用的方法 当电话状态更改时,系统发送
TelephonyManager.ACTION\u phone\u state\u changed
广播。此广播有一个额外的状态,可以使用TelephonyManager.extra\u state
提取
额外状态可以有三种选择:
EXTRA\u STATE\u IDLE
EXTRA\u STATE\u OFFHOOK
EXTRA\u STATE\u RINGING
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(TelephonyManager.ACTION_CALL_STATE_CHANGED){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE){
// No call currently active.
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING){
// A call is ringing
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
// Call has been answered.
}
}
}
}
您可以在此处阅读更多信息:
<强>注:< /强>广播接收器将在后台触发,如Android O+一样,不能从后台进程启动后台服务。考虑启动带有“代码>上下文”的前台服务。StestFrimeService(意图)< /代码>。在<代码> OnStistCube(…)>代码>服务中,调用<代码>启动前景(…)。否则,系统会引发一个致命的异常。但是,在Android O下,您可以安全地使用
上下文。startService(intent)
而不是startForegroundService(…)
您是指“电话呼叫”、“方法呼叫”还是其他呼叫?(交配呼叫?谢幕呼叫?Lauren Bacall?:-)安装此应用程序时,手机收到呼叫时出现一个问题…在调试模式下,控件不会打开ReceiveVaibhav..在呼叫状态响起后:i put system.out.println(“rining”)我在电话中设置了一个断点..并进行了调试..我从另一部电话打电话..状态正在响..断点不应该被击中。?情况并非如此..第一次电话铃响时检查logcat case TelephonyManager.CALL_state_Ring:它打印logcat一次…第二次呼叫它打印两次…第三次…它是这样的,这是因为你正在泄漏listeners.starting Android O您需要获得应用程序内权限才能读取手机状态我认为这是一个更好的解决方案。在第一种情况下,每次手机状态更改都会添加一个新的侦听器。+1我不知道该方法存在。但是,只有当我们想知道特定时间的通话状态时,该方法才有帮助,但它无助于监控手机状态始终处于所有状态。((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getCallState()用于新手
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(TelephonyManager.ACTION_CALL_STATE_CHANGED){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE){
// No call currently active.
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING){
// A call is ringing
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
// Call has been answered.
}
}
}
}