Cordova拨号获取通话时长

来源:互联网 发布:淘宝怎么搜原味胖次 编辑:程序博客网 时间:2024/06/11 20:01

Android关于获取拨出号码的通话时间

由于最近公司项目中要接入cordova插件,在使用第三方CordovaCallNumberPlugin打电话插件,因为需求中需要打电话的时长,自己在CordovaCallNumberPlugin中看了,也没有这个功能,看来也只能自己动手改了。

插件地址:
https://github.com/Rohfosho/CordovaCallNumberPlugin

安装插件:
cordova plugin add https://github.com/Rohfosho/CordovaCallNumberPlugin

权限:

<uses-permission android:name="android.permission.CALL_PHONE" /><uses-permission android:name="android.permission.READ_CALL_LOG"/><uses-permission android:name="android.permission.WRITE_CALL_LOG"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/>

替换上下文就能在cordova里面用了

1.通过电话TelephonyManager设置监听

telephonyManager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);    telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); 

2.创建电话监听器

private boolean flag;private PhoneStateListener listener = new PhoneStateListener() {    @Override    public void onCallStateChanged(int state, String incomingNumber) {        //注意,方法必须写在super方法后面,否则incomingNumber(来电号码)无法获取到值。        super.onCallStateChanged(state, incomingNumber);        switch (state) {            case TelephonyManager.CALL_STATE_IDLE://挂断                if (flag) {//软件运行就会监听到挂断,不知道为什么                    new Thread(new Runnable() {                        @Override                        public void run() {                            try {                                Thread.sleep(1000);                                ContentResolver contentResolver = getContentResolver();                                Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI,//系统方式获取通讯录存储地址                                        new String[]{CallLog.Calls.DURATION//通话时长                                                , CallLog.Calls.TYPE  //呼出                                                , CallLog.Calls.DATE//拨打时间                                        }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);                                if (null != cursor && cursor.getCount() > 0) {                                    if (cursor.moveToFirst()) {                                        final long duration = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DURATION));                                        runOnUiThread(new Runnable() {                                            @Override                                            public void run() {                                                tvCallTime.setText("通话时长:" + duration);                                            }                                        });                                        Looper.prepare();                                        Toast.makeText(MainActivity.this, duration + "", Toast.LENGTH_SHORT).show();                                        Looper.loop();                                    }                                } else {                                    //获取通话记录失败                                }                                telephonyManager.listen(listener,LISTEN_NONE);//注销监听                                if (cursor != null) {                                    cursor.close();                                }                            } catch (InterruptedException e) {                                e.printStackTrace();                                telephonyManager.listen(listener,LISTEN_NONE);//注销监听                                Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();                            }                        }                    }).start();                }                flag = true;                break;        }    }};

可以看到上面,在监听到的状态为CALL_STATE_IDLE时,在通过ContentResolver对象在数据库中查询,再挂断后获取通话记录时可能还没有写入到系统数据库,所以在线程中睡眠了1秒;每次操作完后并注销监听。哈哈 第一次写博客,写的不好,来打我呀。


1 0