Android如何使用呼叫类型和号码来结束呼叫结束?

时间:2014-08-27 14:10:23

标签: android service listener

可以有人给我示例或链接:

如何通过呼叫类型(呼出,呼入)和呼叫者数量(被呼叫的号码或接收到的号码)来结束呼叫结束?

感谢您的任何建议。

编辑:

权限是愚蠢的:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

接收器是这样定义的:

<!-- RECEIVER FOR WATCHING INCOMMING AND OUTGOING CALLS -->
        <receiver
            android:name=".CallReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="1">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter android:priority="2">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

收到:

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        String incomingPhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); //store incoming number
        String outgoingPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


        if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {
            Log.i("TEST","Outgoing " + outgoingPhoneNumber);
            Log.i("TEST","Incoming" + incomingPhoneNumber);

        } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
            Log.i("TEST","Offhooked");
        }
    }

1 个答案:

答案 0 :(得分:1)

来电 - 您可以在广播接收器中使用EXTRA_STATE_IDLE意图来捕捉来电的结束,如下所示 -

public void onReceive(Context context, Intent intent) {

        state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
      if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
       { number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i("incoming", ""+number);
       Toast.makeText(context, ""+number, Toast.LENGTH_LONG).show();
       }
       if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
          { outgoingPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
       Log.i("outgoing", ""+outgoingPhoneNumber);
        // Toast.makeText(context, ""+outgoingPhoneNumber, Toast.LENGTH_LONG).show();
        }
        if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {   
            //Do whatever you like

        }   

}

和以下代码

if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))
            {   
            Log.i("TEST","Outgoing " + outgoingPhoneNumber);
            Log.i("TEST","Incoming" + incomingPhoneNumber);

            }   

将始终返回null,因为每当状态更改onReceive将再次被调用时,此时该数字将为null。因此,如果您想在呼叫结束后访问该号码,请使用共享偏好设置存储它,稍后在EXTRA_STATE_IDLE

中检索

你可以在这里找到一本写得很好的教程 - Call Detection