手机监听器的状态多次调用 [英] Phone state listener called multiple times

查看:271
本文介绍了手机监听器的状态多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想进入的号码登录到数据库。我使用的广播接收器监听电话和用手机状态监听器。 这里是我的code

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

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

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

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()为什么它被称为多次。我想插入来电号码数据库只有一次。

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

推荐答案

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

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

这篇关于手机监听器的状态多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆