对于uni-app的开发,很多地方都需要h5+接口去实现,对于需要切入原生方式开发的,因为对Android 原生开发不了解,所以就显得很无奈。因业务需求,需要监听电话状态,找了好久天才找到,特意来分享一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 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 phonetype=telManager.getCallState(); let phoneNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER); console.log("phonetype:"+phonetype); console.log(); switch (phonetype) { case 0: _this.getCallLog(); console.log('aaa') break; case 1: console.log('bbb') break; case 2: console.log('ccc') break; }
} }); let IntentFilter = plus.android.importClass('android.content.IntentFilter'); let filter = new IntentFilter(); filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED); maintest.registerReceiver(receiver, filter);
|