gpt4 book ai didi

android - 获取当前来电和去电的电话号码

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:41 24 4
gpt4 key购买 nike

我正在制作一个应用程序,我必须在其中检索电话号码并且我能够检索它但我的问题是我正在获取上次通话电话号码> 而不是 present call。我的代码如下:

projection = new String[]{Calls.NUMBER};
cur = context.getContentResolver().query(Calls.CONTENT_URI, projection, null, null, null);
cur.requery();
numberColumn = cur.getColumnIndex(Calls.NUMBER);
cur.requery();
cur.requery();
cur.requery();
cur.requery();
cur.moveToLast();
s = cur.getString(numberColumn);
String pathname = "/sdcard/" + s + ".amr";

最佳答案

正如 Krutix 所建议的,调用日志是已完成电话的日志,在调用结束后由拨号器应用程序写入。因此,您不会在内容提供商中找到当前正在拨叫的号码。

这是一个实现,它允许您检索电话号码,如果它是来电,则作为 incomingNumber 以及当调用结束时 - 请注意 Handler() 代码。

private class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

@Override
public void onCallStateChanged(int state, String incomingNumber) {

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[]{Calls.NUMBER};
Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
cur.moveToFirst();
String lastCallnumber = cur.getString(0);
}
},500);

isPhoneCalling = false;
}

}
}
}

然后在您的 onCreate 或 onStartCommand 代码中添加并初始化监听器:

    PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);

关于android - 获取当前来电和去电的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10136490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com