uni-app 监听电话状态
对于uni-app的开发,很多地方都需要h5+接口去实现,对于需要切入原生方式开发的,因为对Android 原生开发不了解,所以就显得很无奈。因业务需求,需要监听电话状态,找了好久天才找到,特意来分享一下:
let maintest = plus.android.runtimeMainActivity();
let Contexttest = plus.android.importClass("android.content.Context");
let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
let telManager=plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
let receiver=plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(Contexttest, intent) {
plus.android.importClass(intent);
console.log(intent.getAction());
// let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
// let telephonyManager=plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
let phonetype=telManager.getCallState();
let phoneNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER);
console.log("phonetype:"+phonetype); //电话状态 0->空闲状态 1->振铃状态 2->通话存在
console.log(); //电话号
switch (phonetype) {
case 0:
_this.getCallLog();
console.log('aaa')
break;
case 1:
console.log('bbb')
break;
case 2:
console.log('ccc')
break;
}
// _t.state.push({state:phonetype == 0?'空闲状态':phonetype == 1?'振铃状态':'通话存在', time:dateFormat('hh:mm:ss')})
}
});
let IntentFilter = plus.android.importClass('android.content.IntentFilter');
let filter = new IntentFilter();
filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED);
maintest.registerReceiver(receiver, filter);
希望对各位有点帮助哦,关注一下,不至于需要的额时候找不到