如何从NativeScript(LAC,CID,MCC,MNC)调用Android Api

时间:2019-04-11 12:51:53

标签: nativescript

我在Android Studio中创建了以下代码:

 public void onClick(View v) {

        final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {


            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            final GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation();
            if (location != null) {

                Toast.makeText(MainActivity.this,"LAC: " + location.getLac() + " CID: " + location.getCid(),Toast.LENGTH_LONG).show();
            }
        }
    }

如果NativeScript拥有100%的访问权限,那么Android API如何在.ts NativeScript中达到相同的结果?

2 个答案:

答案 0 :(得分:3)

请参阅Java to JS上的文档,以了解如何将本机api调用编组到JS / TS中。

您将可以使用application.android.foregroundActivity从应用程序模块获取当前活动。因此,下面的代码应返回电话管理器的实例。

const telephony = application.android.foregroundActivity.getSystemService(android.content.Context.TELEPHONY_SERVICE)

使用nativescript-permissions插件来获取权限或了解您的应用是否已在设备上具有所需的权限。

还有nativescript-toast插件,可实现跨平台敬酒消息。

答案 1 :(得分:0)

感谢Manoj的有用帮助。 因此需要采取的步骤是:

  1. 安装: npm install tns-platform-declarations --save-dev
  2. 在reference.d.ts中添加两行,在我的情况下,它位于文件ProjectName\references.d.ts

    /// <reference path="node_modules/tns-platform-declarations/android.d.ts" />

    /// <reference path="node_modules/tns-platform-declarations/ios.d.ts" />

  3. 安装nativescript-permissions插件:tns plugin add nativescript-permissions

  4. 安装nativescript-toast插件$ tns plugin add nativescript-toast

  5. 在文件开头导入库

    var application = require("application");

    const permissions = require( "nativescript-permissions" );

    import * as Toast from 'nativescript-toast';

  6. 并编写代码以获取LAC,CID,MCC,MNC

buttonGetInfoTap(args) { 
    const telephonyService = application.android.foregroundActivity.getSystemService(android.content.Context.TELEPHONY_SERVICE);
    permissions.requestPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION, "I need these permissions because I'm cool")
    .then( () => {
      if (telephonyService.getPhoneType() == android.telephony.TelephonyManager.PHONE_TYPE_GSM) {
        let location = <android.telephony.gsm.GsmCellLocation>telephonyService.getCellLocation();
        if (location != null) {
            console.log("LAC: " + location.getLac() + " CID: " + location.getCid())
            Toast.makeText("LAC: " + location.getLac() + " CID: " + location.getCid(), "long").show();
            const networkOperator = telephonyService.getNetworkOperator();
            const MCC =networkOperator.substring(0,3)
            const MNC =networkOperator.substring(3)
            Toast.makeText("MCC:"+MCC+"MNC:"+MNC, "long").show();
        }
      }
    })
    .catch( () => {
      Toast.makeText("No permission", "long").show();
    });
}