在TelephonyManager类中,有一个常数可以知道呼叫是否正在振铃(CALL_STATE_RINGING),是否有类似的常量来知道是否未接收到呼叫?我正在使用以下代码在呼叫振铃时执行任务但我希望该任务仅在用户未接听电话时运行。
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.v("ranjith", "Entered in callstatelistener");
答案 0 :(得分:2)
尝试一下:
在 Manifest.xml :
中<!-- declare the permission -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Register your Broadcast receiver -->
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
在接收器类中:
public class CallReceiver extends BroadcastReceiver {
static boolean isRinging = false;
static boolean isCallReceived = false;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Get the current Phone State
String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(phoneState == null)
return;
// If phone is "Rininging"
if(phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
isRinging = true;
}
// If an incoming call is received
if(phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
isCallReceived = true;
}
// if phone is idle
if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)){
// detect call not received
if(isRinging == true && isCallReceived == false){
Toast.makeText(context, "Call was not received!", Toast.LENGTH_LONG).show();
}
}
}
}