cordova 插件权限在我的应用程序中不起作用
cordova plugin permission not working in my application
朋友提前致谢。这是我的第一个问题。
我不是 Cordova 开发人员,但由于某些情况,我必须处理 Cordova 应用程序。
我的应用程序之前的目标是 Android API 级别 22,现在我的目标是 API 级别 26。
从上面的 API 级别开始,22 androids 需要运行时权限,我正在尝试在我的应用程序中实现权限代码。
我需要将 pdf 文件从我的应用程序复制到设备内存。我已经编写了复制文件的代码,它在 android API 级别 22 之前工作正常,但在 android API 级别 23 以上无法正常工作。
为此,我需要在我的 Cordova 应用程序中添加权限。
我已使用以下插件获得权限 cordova plugin add cordova-plugin-permission
以下是我的 index.js 代码
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
//deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
// **my permission code**
var Permission = cordova.plugins.Permission
var permission = 'android.permission.WRITE_EXTERNAL_STORAGE'
Permission.has(permission,function(results){
if(!results[permission])
{
Permission.request(permission,function(results){
if(results[permission]){
alert("permission granted");
}
},alert("permission failed"))
alert("permission granted failed");
}
}, alert("permission failed"))
asset2sd.copyDir({
asset_directory: "www/pdf",
destination_directory: "crisispdf",
},
function () {
//alert('success');
},
function () {
//alert('fail');
}
);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
console.log('Received Event: ' + id);
}
};
app.initialize();
以下是我的config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.atlascopco.crisis" version="1.0.13" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Crisis Management</name>
<description>
Crisis Management
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Atlas Copco Team
</author>
<content src="index.html" />
<access origin="*" />
<access launch-external="yes" origin="mailto:*" />
<access launch-external="yes" origin="tel:*" />
<plugin name="cordova-plugin-inappbrowser" spec="^1.7.1" />
<plugin name="cordova-plugin-wkwebview-engine" spec="^1.1.3" />
<plugin name="cordova-plugin-permission" spec="^0.1.0" />
<platform name="android">
<uses-permission android:maxSdkVersion="26" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:maxSdkVersion="26" android:name="android.permission.READ_EXTERNAL_STORAGE" />
</platform>
<engine name="android" spec="^7.1.4" />
</widget>
以下是创建调试 apk
后来自 android 平台的我的 Android 清单文件
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10013" android:versionName="1.0.13" package="com.atlascopco.crisis" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
请告诉我在 chrome 浏览器中检查时我的代码有什么问题它显示以下结果并且在应用程序启动时没有获得权限弹出窗口 enter image description here
试试这个代码
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
//deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
// **my permission code**
var permission = cordova.plugins.permissions;
permission.hasPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
if(!results[permission])
{
permission.requestPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
if(results[permission]){
alert("permission granted");
}
},alert("permission failed"))
alert("permission granted failed");
}
}, alert("permission failed"))
asset2sd.copyDir({
asset_directory: "www/pdf",
destination_directory: "crisispdf",
},
function () {
//alert('success');
},
function () {
//alert('fail');
}
);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
console.log('Received Event: ' + id);
}
};
app.initialize();
朋友提前致谢。这是我的第一个问题。 我不是 Cordova 开发人员,但由于某些情况,我必须处理 Cordova 应用程序。 我的应用程序之前的目标是 Android API 级别 22,现在我的目标是 API 级别 26。
从上面的 API 级别开始,22 androids 需要运行时权限,我正在尝试在我的应用程序中实现权限代码。
我需要将 pdf 文件从我的应用程序复制到设备内存。我已经编写了复制文件的代码,它在 android API 级别 22 之前工作正常,但在 android API 级别 23 以上无法正常工作。
为此,我需要在我的 Cordova 应用程序中添加权限。
我已使用以下插件获得权限 cordova plugin add cordova-plugin-permission
以下是我的 index.js 代码
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
//deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
// **my permission code**
var Permission = cordova.plugins.Permission
var permission = 'android.permission.WRITE_EXTERNAL_STORAGE'
Permission.has(permission,function(results){
if(!results[permission])
{
Permission.request(permission,function(results){
if(results[permission]){
alert("permission granted");
}
},alert("permission failed"))
alert("permission granted failed");
}
}, alert("permission failed"))
asset2sd.copyDir({
asset_directory: "www/pdf",
destination_directory: "crisispdf",
},
function () {
//alert('success');
},
function () {
//alert('fail');
}
);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
console.log('Received Event: ' + id);
}
};
app.initialize();
以下是我的config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.atlascopco.crisis" version="1.0.13" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Crisis Management</name>
<description>
Crisis Management
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Atlas Copco Team
</author>
<content src="index.html" />
<access origin="*" />
<access launch-external="yes" origin="mailto:*" />
<access launch-external="yes" origin="tel:*" />
<plugin name="cordova-plugin-inappbrowser" spec="^1.7.1" />
<plugin name="cordova-plugin-wkwebview-engine" spec="^1.1.3" />
<plugin name="cordova-plugin-permission" spec="^0.1.0" />
<platform name="android">
<uses-permission android:maxSdkVersion="26" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:maxSdkVersion="26" android:name="android.permission.READ_EXTERNAL_STORAGE" />
</platform>
<engine name="android" spec="^7.1.4" />
</widget>
以下是创建调试 apk
后来自 android 平台的我的 Android 清单文件<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10013" android:versionName="1.0.13" package="com.atlascopco.crisis" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
请告诉我在 chrome 浏览器中检查时我的代码有什么问题它显示以下结果并且在应用程序启动时没有获得权限弹出窗口 enter image description here
试试这个代码
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
//deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
// **my permission code**
var permission = cordova.plugins.permissions;
permission.hasPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
if(!results[permission])
{
permission.requestPermission(permission.WRITE_EXTERNAL_STORAGE,function(results){
if(results[permission]){
alert("permission granted");
}
},alert("permission failed"))
alert("permission granted failed");
}
}, alert("permission failed"))
asset2sd.copyDir({
asset_directory: "www/pdf",
destination_directory: "crisispdf",
},
function () {
//alert('success');
},
function () {
//alert('fail');
}
);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
console.log('Received Event: ' + id);
}
};
app.initialize();