【问题标题】:Get phone number of present incoming and outgoing call获取当前来电和去电的电话号码
【发布时间】:2012-04-25 13:28:27
【问题描述】:

我正在制作一个应用程序,我必须在其中检索电话号码并且我能够检索它,但我的问题是我获得了 last 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";

【问题讨论】:

标签: android phone-number


【解决方案1】:

根据 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);

【讨论】:

  • 编译时错误:getContentResolver() 方法未为类型 new Runnable(){} 定义?
  • 通过指定字段private Context context解决了;和 context.getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE + "desc");参考:stackoverflow.com/questions/10165609/…
  • 不要存储 Context 类型的私有字段。相反,使用 Wea​​kReference someName,并且,当您需要使用上下文时,调用 someName.get() - 这将返回一个上下文。在存储上下文的地方,只需使用 new WeakReference(context); 创建weakReference
【解决方案2】:

请修改您的查询

 cur = context.getContentResolver().query(Calls.CONTENT_URI,
                        projection, null, null, Calls.DATE +" desc");
  cur.moveToFirst();
  s = cur.getString(numberColumn);

它会给出最后一次通话的电话号码,不需要重复查询那么多次

【讨论】:

  • 我仍然得到以前的号码
  • 使用我的代码,您可以在通话结束后获取数据,并且在通话响铃或保持时您需要该信息,您必须使用监听器和接收器
  • 你能帮忙举个例子吗
  • 你在你的代码中到底想要什么,因为桑迪已经发布了代码可以帮助你的链接
  • 你到底想要什么我不明白?
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2016-09-06
  • 2011-11-04
  • 1970-01-01
  • 2015-07-22
  • 2013-03-02
相关资源
最近更新 更多