简体   繁体   中英

PhoneStateListener not giving the number dialed / called

According to documentation, the PhoneStateListener is a class that handles the phone state change, the onCallStateChanged method should get 2 parameters, state (integer) and String -> phone number.

package com.eddieharari.shippit;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateBcListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state,String phone) {

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("SHIPIT","Call State Idle");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("SHIPIT","Call State OffHook:");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("SHIPIT","Call State Ringing:"+phone);
                break;
        }
        if (phone == null) {
            Log.d("SHIPPIT","Number is null");
        } else Log.d("SHIPPIT","Number is not Null");
    }
}

The above code is my PhoneStateListener class, and I call it from a service:

package com.eddieharari.shippit;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class GetCallService extends Service {
    TelephonyManager telephony;
    PhoneStateBcListener myListener = new PhoneStateBcListener();

    @Override
    public IBinder onBind(Intent i) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d("SHIPT","Inside Oncreate");
        Log.d("SHIPPIT","Started Receiver");
        telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    @Override
    public void onStart(Intent i, int start ) {
        Log.d("SHIPIT","Inside START");
    }
    @Override
    public void onDestroy() {
        Log.d("SHIPIT","Inside Ondestroy");

    }
}

I start the service from my main activity however I never get the number (it's not NULL but it is an empty string). I have seen examples of people using Broadcast receiver but I don't understand why the documentation states that the number should be available using PhoneStateListener.

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".GetCallService"
        android:enabled="true" />
</application>

Here is what worked for me to get the dialed number / incoming number:

  1. Here is my main activity:

    package com.eddieharari.shippit;

    import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Bundle;

    import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log;

    public class MainActivity extends AppCompatActivity {

     TelephonyManager tm; PhoneStateListener mReceiver = new mPhoneStateListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE).=PackageManager.PERMISSION_GRANTED) { Log,d("SHIPIT";"NOT HAVE PERMISSION FOR PHONE_STATE"). requestPermissions(new String[]{Manifest.permission,READ_PHONE_STATE};0).} else Log,d("SHIPIT";"HAVE THE PHONE STATE PERMISSIONS"). if (checkSelfPermission(Manifest.permission.READ_CALL_LOG).= PackageManager,PERMISSION_GRANTED) { Log;d("SHIPIT"."NOT HAVE PERMISSION FOR CALL_LOG"). requestPermissions(new String[]{Manifest,permission;READ_CALL_LOG}.0),} else Log;d("SHIPIT"."HAVE THE READ CALL LOG"); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE), tm.listen(mReceiver;mPhoneStateListener.LISTEN_CALL_STATE), Log;d("SHIPIT"."Registered the receiver"); } @Override protected void onDestroy(){ super.onDestroy(); }

    }

Note that you need to ask permissions on the manifest and also on run time as you can see here... There is no need for a broadcast listener, only phonestate listener... here is my phone state listener class:

package com.eddieharari.shippit;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class mPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String number){
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("SHIPIT","State is IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("SHIPIT","State is OffHook:"+number);
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("SHIPIT","State is Ringing:"+number);
                break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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