Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone state listener called multiple times

Tags:

android

hi i want to log the incoming number to the database. i am using broadcast receiver to listen for phone calls and using phone state listener. here is my code

ThePhoneStateListener.java

package com.example.netlogger.Receiver;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class ThePhoneStateListener extends PhoneStateListener {
    Context context;
    public ThePhoneStateListener(Context context) {
        this.context = context;
    }
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            Log.d("TAGG", "Insert: " + incomingNumber);
        }

    }
}

CallActionReceiver.java

package com.example.netlogger.Receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class CallActionsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        TelephonyManager manager = (TelephonyManager) arg0
                .getSystemService(arg0.TELEPHONY_SERVICE);
        manager.listen(new ThePhoneStateListener(arg0),
                android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
    }
}

Menifest.xml

<receiver android:name="com.example.netlogger.Receiver.CallActionsReceiver">
    <intent-filter android:priority="2147483647">
        <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>
    <intent-filter android:priority="2147483647">
        <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter>
</receiver>

in ThePhoneStateListeneronCallStateChanged() why it is called multiple times. i want to insert incoming number to database just once.

like image 532
Usman Riaz Avatar asked Apr 15 '13 13:04

Usman Riaz


People also ask

What is phone state listener?

About Telephony Manager This class uses a listener called PhoneStateListener to listen for telephony service updates. An object of type TelephonyManager will be instantiated, and it will listen for updates of Context. TELEPHONY_SERVICE . In order to monitor updates of telephony states like PhoneStateListener.

What is call state?

We can also get the information of call state using the TelephonyManager class. For this purpose, we need to call the listen method of TelephonyManager class by passing the PhonStateListener instance. The PhoneStateListener interface must be implemented to get the call state.


2 Answers

Some how , i solved it. the problem is that when ever my broadcast receiver's onReceive() was invoked ,every time i was trying to listen a new PhoneStateListener.You just need to do it once. just like following

package com.example.netlogger.Receiver;

import java.util.Date;

import com.example.netlogger.util.LocalDatabase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class CallActionsReceiver extends BroadcastReceiver {
    static ThePhoneStateListener phoneStateListener;



    @Override
    public void onReceive(Context arg0, Intent arg1) {
        TelephonyManager manager = (TelephonyManager) arg0
                .getSystemService(arg0.TELEPHONY_SERVICE);      
        if (phoneStateListener == null) {
            phoneStateListener = new ThePhoneStateListener(arg0);
            manager.listen(phoneStateListener,
                    android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
        }


    }

}

Problem solved. Cheers

like image 136
Usman Riaz Avatar answered Oct 16 '22 05:10

Usman Riaz


Thank you for your solution, but it worked for me when I implemented singleton object of my PhoneStateListener class. onReceive in my BroadcastReceiver:

@Override
public void onReceive(final Context context, Intent intent) {
    telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    if (phoneListener == null) {
        phoneListener = CallStateListener.getCallStateInstance(context);CallStateListener(context);
        telephony.listen(phoneListener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
    }
}

and this is method in my PhoneStateListener class:

public static CallStateListener getCallStateInstance(Context ctx) {
    if (callStateInstance == null)
        callStateInstance = new CallStateListener(ctx);

    return callStateInstance;
}

hope it will be useful for somebody.

like image 40
Rikki Tikki Tavi Avatar answered Oct 16 '22 05:10

Rikki Tikki Tavi