以编程方式检测传出/传入号码android 5.1.1棒棒糖

时间:2017-05-11 12:10:57

标签: android broadcastreceiver android-5.1.1-lollipop

尝试在(Lollipop 5.1.1)OnePlus两个氧气检测器上检测输出或输入数量

用谷歌搜索尝试了很多方法但是没有成功

String number = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

清单中的权限

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

使用波纹管代码获取传入或传出号码

public class callRecorderReceiver extends BroadcastReceiver {
    private static String TAG = "CallRecorderReceiver";
    private Context mContext = null;
    static long start_time, end_time;
     String callType = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        this.mContext = context;
        String action = intent.getAction();
        String extraState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        Log.d(TAG, "Action:" + action);
        Log.d(TAG, "ExtraState:" + extraState);

         String number = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
            if (number == null || number.equalsIgnoreCase("")) {
                number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            }

        if (extraState.equalsIgnoreCase("OFFHOOK")) {

            if (number != null && !number.equalsIgnoreCase("")) {
                number = number;
                temp_data.current_number = "" + number;
            }

        } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {

            if (number != null && !number.equalsIgnoreCase("")) {
               callType = "outgoing";
                if (!((number.contains("*") && number.contains("#")) ))) {
                    Log.d("outgoing CallTo:", number);

                }
                            }
        } else if (action.equals("android.intent.action.PHONE_STATE")) {
            try {
                String incomingNumber = intent.getStringExtra("incoming_number");

                if (intent.getStringExtra("state").equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                    Log.d("ringing ", "incomingNumber " + incomingNumber);
                } else if (intent.getStringExtra("state").equals(TelephonyManager.EXTRA_STATE_IDLE)) {

                    Log.d("idle ", incomingNumber);
                } else if (intent.getStringExtra("state").equals(TelephonyManager.EXTRA_STATE_OFFHOOK)))) {
                    Log.d("State:", "current_state:" + temp_data.current_state);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


}

2 个答案:

答案 0 :(得分:0)

检查此链接.... [http://bitgriff.blogspot.in/2013/01/detecting-incoming-and-outgoing-phone.html]我希望它会让你感到高兴

答案 1 :(得分:0)

所以理由是onReceive()方法没有调用,首先检查设备的版本,如果设备大于lolipop,请转到设置&gt;应用&gt;当前的工作应用&gt;给予电话许可然后它会为你工作。