0

I have been trying to run an app that would listen to phone_state calls. However it does not seem to do it. Either it does, but it takes along time to finally listen, or it does not listen at all.

This has been bugging me the whole time since it works on all devices I have tested on except Sony Xperia Z5. I have added the necessary permissions and runtime permissions on the Activity. What the main purpose is to call a number from the app. And then listen to state changes on the phone.

Code for BroadcastReceiver:

public class OutgoingCallReceiver extends BroadcastReceiver {

    static CallPromptView mCallPromptView;
    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            if(!TextUtils.isEmpty(number)){
                setPrefPhoneNumber(number);
            }
        }
        else if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
            String number = getPrefPhoneNumber();
            if(tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK ||
                    tm.getCallState() == TelephonyManager.CALL_STATE_RINGING) {
                Log.d("STATE", "Phonestate : " + tm.getCallState());
            }else if(tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
                Log.d("STATE", "Phonestate idle");
                }
            }
        }
    }
}

AndroidManifest.xml:

    <!-- Phone call permissions -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <!-- GPS permissions -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".ui.MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".receiver.OutgoingCallReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />
    </application>

    <!-- Window permissions -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Khiem-Kim Ho Xuan
  • 1,301
  • 1
  • 22
  • 45

0 Answers0