nativescript-android: "request location permissions on runtime" 的回调

nativescript-android: callback of "request location permissions on runtime"

所以我向 android phone 请求使用其位置的许可。我得到拒绝或允许的对话框,这很好。 问题是我不知道如何捕捉到它的响应。我的代码如下:

var location_permissions = [ 
        (android as any).Manifest.permission.ACCESS_COARSE_LOCATION,
        (android as any).Manifest.permission.ACCESS_FINE_LOCATION
    ];

(android.support.v4.app.ActivityCompat as any).requestPermissions( 
    app.android.foregroundActivity,
    location_permissions,
    "PERMISSION_LOCATION_REQUEST_CODE"
);

// var grantResults = [];

// (app.android.foregroundActivity as any).onRequestPermissionsResult(
//     app.android.foregroundActivity,
//     "PERMISSION_LOCATION_REQUEST_CODE",
//     location_permissions,
//     grantResults
// );

我猜有一个关于使用 onRequestPermissionsResult 作为回调的文档? (https://developer.android.com/training/permissions/requesting.html) 我在那里写了它,但不确定应该如何正确实施它。因为这是用打字稿用angular2写的,我想我可以用.then捕捉到响应,但它会抛出一个错误:cannot read property then of undefined

检查插件代码并从中得到我想要的:

var location_permissions = [ 
        (android as any).Manifest.permission.ACCESS_FINE_LOCATION // no need to define "ACCESS_COARSE_LOCATION", because "ACCESS_FINE_LOCATION" includes permissions to NETWORK_PROVIDER and GPS_PROVIDER
    ];

var activityRequestPermissionsHandler_location = function (data) {
    //console.log('requestCode: ' + data.requestCode + ' permissions: ' + data.permissions + ' grantResults: ' + data.grantResults);
    if (data.requestCode === 1 ) {
        if (data.grantResults.length > 0 && data.grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED) {
            console.log("permission granted!!!");
        }
        else {
            console.log("permission not granted!!!");
        }
    }
    app.android.off(app.AndroidApplication.activityRequestPermissionsEvent, activityRequestPermissionsHandler_location);
};
app.android.on(app.AndroidApplication.activityRequestPermissionsEvent, activityRequestPermissionsHandler_location);

(android.support.v4.app.ActivityCompat as any).requestPermissions( 
    app.android.foregroundActivity,
    location_permissions,
    1
);