简体   繁体   English

如何知道Android中是否未收到电话

[英]How to know if the call is not received in Android

In TelephonyManager class there is a constant to know if the call is ringing(CALL_STATE_RINGING), is there a similar constant to know if the call is not received? 在TelephonyManager类中有一个常量来知道呼叫是否正在振铃(CALL_STATE_RINGING),是否有类似的常量来知道是否未接收到呼叫? I am using below code to perform a task when call is ringing but I want the task to run only when user does not pick up the call. 我正在使用下面的代码在呼叫振铃时执行任务,但我希望任务仅在用户不接听电话时运行。

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.v("ranjith", "Entered in callstatelistener");

Have a try with it: 尝试一下:

In your Manifest.xml : 在你的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>

And in your receiver class: 在你的接收器类:

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();
       }
   }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM