电话状态监听器多次调用

时间:2013-04-15 13:36:43

标签: android

嗨我想将传入号码记录到数据库中。我正在使用广播接收器来收听电话和使用电话状态监听器。 这是我的代码

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>

ThePhoneStateListener onCallStateChanged()中为什么多次调用它。我想将传入号码只插入数据库一次。

3 个答案:

答案 0 :(得分:28)

有些怎么样,我解决了。问题是,当我的广播接收器的onReceive()被调用时,每次我试图听一个新的PhoneStateListener。你只需要做一次。就像下面的

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);
        }


    }

}

问题解决了。干杯

答案 1 :(得分:3)

感谢您的解决方案,但是当我实现PhoneStateListener类的单例对象时,它对我有用。我的BroadcastReceiver中的onReceive

@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);
    }
}

这是我的PhoneStateListener类中的方法:

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

    return callStateInstance;
}

希望它对某些人有用。

答案 2 :(得分:0)

我只是创建静态变量并检查

 public class IncomingCallReceiver extends BroadcastReceiver
{
    private static String canEndCall;

    @Override
    public void onReceive(final Context context, Intent intent)
    {
        if (canEndCall == null)
        {
            TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            tmgr.listen(new PhoneStateListener()
            {
                @Override
                public void onCallStateChanged(int state, String incomingNumber)
                {

                }
            }, PhoneStateListener.LISTEN_CALL_STATE);
            canEndCall = "...";
        }
    }