简体   繁体   English

Cordova 中的 android 权限问题

[英]Issues with android permissions in Cordova

I'm working on a demo app for android that needs to get information about the phone, the SIM, the network, etc.. I put all the code in a plugin and my Java function that that retrieves the SIM info looks like this:我正在为 android 开发一个演示应用程序,它需要获取有关手机、SIM 卡、网络等的信息。我将所有代码放在插件中,我的 Java ZC1C425268E68385D14AB5074C17 SIMA 信息看起来像这样4:F14AB5074C17 SIMA 信息


  private boolean getSIMInfo(CallbackContext cbc) throws JSONException {

    // tm is a TelephonyManager instantiated in initialize
    JSONObject res = new JSONObject()
      .put("carrierID", tm.getSimCarrierId()) 
      .put("carrierName", tm.getSimCarrierIdName())
      .put("countryIso", tm.getSimCountryIso())
      .put("operator", tm.getSimOperator())
      .put("operatorName", tm.getSimOperatorName())
      .put("state", tm.getSimState())
      .put("msisdn", tm.getLine1Number()) // <== Requires permission
      ;
    cbc.success(res);
    return true;
  }

As long as I dont call getLine1Number() everything goes fine.只要我不调用getLine1Number()一切都会好起来的。

getLine1Number() requires either android.permission.READ_PHONE_STATE or apREAD_SMS or apREAD_PHONE_NUMBERS to be set. getLine1Number()需要设置android.permission.READ_PHONE_STATEapREAD_SMSapREAD_PHONE_NUMBERS I first declared apREAD_PHONE_STATE in plugin's plugin.xml and I checked it was injected into AndroidManifest.xml .我首先在插件的plugin.xml中声明apREAD_PHONE_STATE并检查它是否已注入AndroidManifest.xml

The platform part of the plugin.xml looks like this:插件的平台部分plugin.xml看起来像这样:

  <platform name="android">

    <config-file target="res/xml/config.xml" parent="/*">
      <feature name="TelPlugin">
        <param name="android-package" value="org.buguigny.CordovaPlugin.TelPlugin"/>
      </feature>
    </config-file>

    <config-file parent="/*" target="AndroidManifest.xml" />
    <config-file target="AndroidManifest.xml" parent="/*">
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    </config-file>
    <source-file src="src/TelPlugin.java" target-dir="src/org/buguigny/CordovaPlugin/TelPlugin" />

  </platform>`

Upon execution I get the error:执行后我收到错误:

getLine1NumberForDisplay: Neither user 10190 nor current process has
   android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or
   android.permission.READ_PHONE_NUMBERS

I tried with the other permissions: same error.我尝试了其他权限:同样的错误。

Any idea what I'm doing wrong?知道我做错了什么吗?

On Android >=6.0, Android app should request permission runtime.在 Android >=6.0 上,Android 应用程序应请求权限运行时。

So in Cordova, you can use the permission plugin like following所以在 Cordova 中,你可以使用如下的权限插件

permissions.requestPermission(permissions.READ_PHONE_STATE, success, error);

function error() {
  console.warn('Camera permission is not turned on');
}

function success( status ) {
  if( !status.hasPermission ) error();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM