获得present呼入和呼出的电话号码 [英] Get phone number of present incoming and outgoing call

查看:141
本文介绍了获得present呼入和呼出的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序中,我需要检索的电话号码,我能够找回它,但我的问题是,我得到的电话 最后通话而不是 present通话。我的code是如下:

 投影=新的String [] {Calls.NUMBER};
CUR = context.getContentResolver()查询(Calls.CONTENT_URI,投影,NULL,NULL,NULL);
cur.requery();
numberColumn = cur.getColumnIndex(Calls.NUMBER);
cur.requery();
cur.requery();
cur.requery();
cur.requery();
cur.moveToLast();
S = cur.getString(numberColumn);
字符串路径=/ SD卡/+ S +.AMR;
 

解决方案

所建议的Krutix,通话记录,是这是由拨号程序后,通话结束写入完成电话呼叫日志中。因此,你不会找到哪个号码正在拨打的内容提供商。

下面是一个实现,这将使您检索的电话号码,如果它是一个呼入电话为incomingNumber,也当通话结束 - 注意处理程序()code

 私有类PhoneCallListener扩展PhoneStateListener {

    私人布尔isPhoneCalling = FALSE;

    @覆盖
    公共无效onCallStateChanged(INT状态,串incomingNumber){

        如果(TelephonyManager.CALL_STATE_RINGING ==州){
            //电话铃声
            Log.i(LOG_TAG,振铃,号码:+ incomingNumber);
        }

        如果(TelephonyManager.CALL_STATE_OFFHOOK ==州){
            // 活性
            Log.i(LOG_TAG,摘机);

            isPhoneCalling = TRUE;
        }

        如果(TelephonyManager.CALL_STATE_IDLE ==州){
            //当类初始和电话通话结束运行,需要检测标志
            //从CALL_STATE_OFFHOOK
            Log.i(LOG_TAG,IDLE号);

            如果(isPhoneCalling){

                处理程序处理程序=新的处理程序();

                //将延迟,因为通话记录并不会立即更新时,状态变化
                //拨号器所花的时间一点点地写它500ms的似乎是不够的
                handler.postDelayed(新的Runnable(){

                    @覆盖
                    公共无效的run(){
                        //得到光标开始
                          Log.i(CallLogDetailsActivity,获取日志活动......);
                            的String []投影=新的String [] {Calls.NUMBER};
                            光标CUR = getContentResolver()查询(Calls.CONTENT_URI,投影,NULL,NULL,Calls.DATE +说明)。
                            cur.moveToFirst();
                            串lastCallnumber = cur.getString(0);
                    }
                },500);

                isPhoneCalling = FALSE;
            }

        }
    }
}
 

,然后添加和初始化监听器在你的onCreate或onStartCommand code:

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

I am making an app in which I have to retrieve the phone number and I am able to retrieve it but my problem is that I am getting phone number of last call rather than present call.My code is as follows:

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";

解决方案

As suggested by Krutix, the call LOG, is a LOG of finished phone calls which is written to by the dialer app after a call is finished. As such you will not find which number is currently dialing in the content provider.

Here is an implementation which will allow you to retrieve the phone number if it is an incoming phone call as incomingNumber and also when the call is FINISHED - note the Handler() code.

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;
            }

        }
    }
}

And then add and initialise the listener in your onCreate or onStartCommand code:

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

这篇关于获得present呼入和呼出的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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