nativescript-android:回调"请求运行时的位置权限"

时间:2016-11-01 09:01:13

标签: android callback permissions location nativescript

所以我要求Android手机获得使用其位置的许可。我得到了拒绝或允许的对话框,这很好。 问题是我不知道如何捕捉到这种反应。我的代码如下:

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)我在那里写了它,但不确定它应该如何实现核心。由于这是用angularcript编写的angular2,我以为我可以用.then捕获响应,但它会抛出一个错误:cannot read property then of undefined

1 个答案:

答案 0 :(得分:0)

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

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
);