嗨,我想将传入的号码记录到数据库中。我正在使用广播接收器来收听电话并使用电话状态监听器。这是我的代码
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);
}
}
清单文件.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() 为什么它被多次调用。我只想将传入的号码插入数据库一次。