【发布时间】:2011-08-22 09:08:29
【问题描述】:
我想知道我是否在通话中。
如果我正在通话,请启动服务(服务部分很清楚)。我该怎么做?
在接听电话时,我需要拨打服务电话...我不知道该怎么做?有什么帮助吗?
【问题讨论】:
-
您是指“电话”、“方法呼叫”还是其他呼叫? (求偶?谢幕?Lauren Bacall?:-))
标签: java android telephonymanager
我想知道我是否在通话中。
如果我正在通话,请启动服务(服务部分很清楚)。我该怎么做?
在接听电话时,我需要拨打服务电话...我不知道该怎么做?有什么帮助吗?
【问题讨论】:
标签: java android telephonymanager
你需要广播接收器...
在清单中声明广播接收器...
<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;
}
}
}
【讨论】:
TelephonyManager.getCallState() 返回其中之一
CALL_STATE_IDLECALL_STATE_OFFHOOKCALL_STATE_RINGING如果这符合您的要求,那么它的代码比 Pied Piper 更全面的解决方案要少得多。
【讨论】:
【讨论】:
毫无疑问,这是一个老问题。但它作为谷歌的第一个结果弹出。因此,我决定添加另一个有用的方法。
当手机状态改变时,系统发送TelephonyManager.ACTION_PHONE_STATE_CHANGED广播。此广播有一个额外的状态,可以使用TelephonyManager.EXTRA_STATE 提取。
额外的状态可以有三种选择:
EXTRA_STATE_IDLEEXTRA_STATE_OFFHOOKEXTRA_STATE_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+ 开始,您无法从后台进程启动后台服务。考虑使用context.startForegroundService(intent) 启动前台服务。在服务的onStartCommand(...),调用startForeground(...)。否则,系统会引发致命异常。但是,在 Android O 之下,您可以安全地使用context.startService(intent) 而不是startForegroundService(...)。
【讨论】: