我想知道我是否在通话中。
如果我正在通话中,则启动服务(服务的部分很清楚)。我该怎么做?
在接听电话时,我需要调用服务... 我不知道如何做?有帮助吗?
我想知道我是否在通话中。
如果我正在通话中,则启动服务(服务的部分很清楚)。我该怎么做?
在接听电话时,我需要调用服务... 我不知道如何做?有帮助吗?
你需要广播接收器 ...
在清单文件中声明广播接收器 ...
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
还要声明 uses-permission...
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
广播接收器类(Broadcast Receiver Class) ...
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_IDLE
:电话处于闲置状态CALL_STATE_OFFHOOK
:电话已摘机CALL_STATE_RINGING
:电话正在响铃如果符合您的要求,与 Piper Pied 的更全面的解决方案相比,它需要的代码量少得多。
你只能知道电话呼叫的到来,但无法修改它。:( 看这个 为什么Android 2.3版本没有android.permission.MODIFY_PHONE_STATE权限?有什么解决方案吗?
这是一个老问题,毫无疑问。但它在谷歌上排名第一。因此,我决定添加另一种有用的方法。
当手机状态改变时,系统会发送TelephonyManager.ACTION_PHONE_STATE_CHANGED
广播。这个广播有一个额外的状态,可以使用TelephonyManager.EXTRA_STATE
提取。
额外的状态可以有三种选择:
EXTRA_STATE_IDLE
EXTRA_STATE_OFFHOOK
EXTRA_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(...)
。