2016-03-22 76 views
0

如何使用android.support.v4.app.ActivityCompat.requestPermissions或android.app.Activity.requestPermissions? 因爲它總是不確定的NativeScript RequestPermission

我想這樣做

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,我們需要問它

我還加上manifest資源配置文件:

<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_Resources \ Android的\ app.gradle:

dependencies { 
    compile "com.android.support:support-v4:+" 
    , compile "com.android.support:appcompat-v7:+" 
} 

感謝

回答

0

您是否嘗試過刪除和添加Android平臺?這裏是一個例子:

tns platform remove android 
tns platform add android 
0

爲了解決這個問題,我們需要升級CLI並重新創建應用程序。

我創建了一個新的應用程序,我從一開始就開始工作! :) 我認爲CLI的升級解決了這個問題,我們需要重新創建一個應用程序,因爲\ 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); 
      });