如何使用Nativescript获得sim网络的信号强度?

时间:2018-07-13 11:54:31

标签: nativescript

import * as application from 'application';
const TnsOneSignal = require('nativescript-onesignal').TnsOneSignal

if (application.android) {
        application.on(application.launchEvent, function(args: application.ApplicationEventData) {
            try {
                console.log('TnsOneSignal', TnsOneSignal)
                TnsOneSignal.startInit(application.android.context).init()
            } catch (error) {
                console.log('error', error)
            }
        })
    }

我尝试使用上面的代码和插件,但是不起作用。

任何人都可以帮助我编写代码或提供更好的解决方案!

1 个答案:

答案 0 :(得分:0)

要获取当前手机的信号强度,您可以在Android和iOS上使用本机API,然后根据NativeScript's marshaling rules将代码转换为JavaScript。

例如,让我们来看看这个native solution。这是NativeScript中的样子

let telephonyManager = application.android.context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
let cellinfogsm = telephonyManager.getAllCellInfo().get(0);
let cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();

当然,在Android中,要获取此信息,您需要获得ACCESS_COARSE_LOCATION的明确许可,以便可以使用nativescript-permissions plugin授予它们。

Here you can find a test project演示了上述技术。基于相同的原理,您可以找到一个iOS示例,并使用从Objective-C到JS的封送处理将代码转换为JS。