Android studio 自定义phonestatelistener只调用几次,然后停止
我想检测一个来电,我想知道电话号码。我还想知道电话什么时候结束 我在这里找到了这个代码: 刚开始的时候效果很好,但是打了几次电话之后,我再也没有收到任何同学的回复 有人能帮我吗?提前谢谢 电话侦听器Android studio 自定义phonestatelistener只调用几次,然后停止,android-studio,telephonymanager,phone-state-listener,Android Studio,Telephonymanager,Phone State Listener,我想检测一个来电,我想知道电话号码。我还想知道电话什么时候结束 我在这里找到了这个代码: 刚开始的时候效果很好,但是打了几次电话之后,我再也没有收到任何同学的回复 有人能帮我吗?提前谢谢 电话侦听器 public class PhoneCallListener extends PhoneStateListener { private static final String LOG_TAG = PhoneCallListener.class.getSimpleName(); pr
public class PhoneCallListener extends PhoneStateListener {
private static final String LOG_TAG = PhoneCallListener.class.getSimpleName();
private boolean isPhoneCalling = false;
private Context context;
PhoneCallListener(Context context) {
this.context = context;
Log.d(LOG_TAG, "PhoneCallListener initiated");
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.d(LOG_TAG, "onCallStateChange " + state);
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE number");
if (isPhoneCalling) {
Handler handler = new Handler();
//Put in delay because call log is not updated immediately when state changed
// The dialler takes a little bit of time to write to it 500ms seems to be enough
handler.postDelayed(new Runnable() {
@Override
public void run() {
// get start of cursor
Log.i("CallLogDetailsActivity", "Getting Log activity...");
String[] projection = new String[]{CallLog.Calls.NUMBER};
@SuppressLint("MissingPermission") Cursor cur = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls.DATE + " desc");
if (cur != null) {
cur.moveToFirst();
String lastCallnumber = cur.getString(0);
Log.d(LOG_TAG, "Call ended: " + lastCallnumber);
cur.close();
}
}
}, 500);
isPhoneCalling = false;
}
}
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private TextView tv;
private Receiver phoneReceiver, smsReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
PhoneCallListener phoneListener = new PhoneCallListener(this);
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
检查您的设备是否有一些电池节电应用程序或类似的内置功能,这些应用程序/设备功能可能会杀死当前未运行的应用程序,阻止它们收到广播通知,并且suchI正在使用模拟器,因此我不认为这是原因。您是否在某个时候强制关闭了应用程序?在这样做之后,没有一个听众或广播接收器会工作,我想是这样的,因为我再试了一次,一切都很好。谢谢是的,常见的困惑问题,除非你已经对此很感兴趣,并且浪费了几个小时试图找出问题出在哪里