我正在为安卓系统开发一个演示应用,需要获取手机、SIM卡、网络等信息。 我把所有的代码放在一个插件和我的Java函数,检索的SIM信息看起来像这样。
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;
}
只要我不调用 getLine1Number()
一切顺利。
getLine1Number()
要求要么 android.permission.READ_PHONE_STATE
或 a.p.READ_SMS
或 a.p.READ_PHONE_NUMBERS
要设置。我首先声明 a.p.READ_PHONE_STATE
在插件的 plugin.xml
我检查了一下,它被注射到 AndroidManifest.xml
.
平台部分 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>`
在执行过程中,我得到了错误信息
getLine1NumberForDisplay: Neither user 10190 nor current process has
android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or
android.permission.READ_PHONE_NUMBERS
我试过用其他权限: 同样的错误。
任何想法,我做错了什么?
在Android >=6.0,Android应用程序应该请求权限运行时。
因此,在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();
}