我如何使用android.support.v4.app.ActivityCompat.requestPermissions或android.app.Activity.requestPermissions? 因为它总是未定义的
我想这样做
var context = android.content.Context;
var wifi_service = application.android.context.getSystemService(context.WIFI_SERVICE);
wifi_service.setWifiEnabled(true);
var rs = wifi_service.startScan();
console.log(android.support.v4.app.ActivityCompat.requestPermissions); // undefined
console.log(android.support.v4.content.ContextCompat.requestPermissions); // undefined
application.android.registerBroadcastReceiver(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION,
function onReceiveCallback(context, intent) {
var tp = wifi_service.getScanResults();
console.log(tp); // empty []
console.log(tp.size()); // 0
});
但是数组tp总是空的,我认为因为我需要正确的权限,而在android 6上我们需要问它
我还添加了manifestfile:
<uses-feature android:name="android.hardware.wifi" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />
还在C上添加:... \ app \ App_Resources \ Android \ app.gradle:
dependencies {
compile "com.android.support:support-v4:+"
, compile "com.android.support:appcompat-v7:+"
}
谢谢
答案 0 :(得分:0)
您是否尝试删除并添加Android平台?这是一个例子:
tns platform remove android
tns platform add android
答案 1 :(得分:0)
要解决此问题,我们需要升级CLI并重新创建应用程序。
我创建了一个新的应用程序,我从一开始就开始工作! :) 我认为CLI的升级解决了这个问题,我们需要重新创建一个app,因为\ node_modules \ tns-core-modules不一样了
这里是修复扫描结果问题的代码:
var context = android.content.Context;
var wifi_service = application.android.context.getSystemService(context.WIFI_SERVICE);
wifi_service.setWifiEnabled(true);
var rs = wifi_service.startScan();
var hasPermission = android.os.Build.VERSION.SDK_INT < 23;
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION);
}
var ACCESS_COARSE_LOCATION_PERMISSION_REQUEST_CODE = 555;
if (!hasPermission) {
android.support.v4.app.ActivityCompat.requestPermissions(
application.android.foregroundActivity,
[android.Manifest.permission.ACCESS_COARSE_LOCATION],
ACCESS_COARSE_LOCATION_PERMISSION_REQUEST_CODE);
}
application.android.registerBroadcastReceiver(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
, function onReceiveCallback(context, intent) {
var tp = wifi_service.getScanResults();
console.log(tp);
});