我有此代码:
function getLocation() {
//function executed if started by webview
if (typeof Jinterface != 'undefined') {
Jinterface.displayGPSRequest();
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError, {
enableHighAccuracy: true
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
在继续执行代码之前,我需要JavaScript等待功能Jinterface.displayGPSRequest()
结束。我尝试了async
/ await
,但由于它是从Android Studio中的Java文件中调用的函数(在那里我可以看到自己的网站的网页视图),因此无法使用async
语句来命名因为Java无法识别它。有帮助吗?
Java函数:
@JavascriptInterface
public void displayGPSRequest() {
Context context = getApplicationContext();
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
final String TAG = "YOUR-TAG-NAME";
final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
});
}
答案 0 :(得分:2)
displayGPSRequest
将需要提供一种知道何时完成的方法。如果当前没有,则需要对其进行编辑以添加一个(或使用轮询,这不是一个好主意)。
通常的方法是:
通过承诺。
通过原始回调。
因此,如果displayGPSRequest
返回承诺(或您将其编辑为):
JInterface.displayGPSRequest()
.then(function() {
// The code that should run when it finishes
})
.catch(function() {
// It failed
});
如果displayGPSRequest
使用原始回调(或您将其编辑为),则:
JInterface.displayGPSRequest(function() {
// The code that should run when it finishes
// It should also have some way of telling the callback it failed
});
如果它没有提供完成时通知您的方法,并且您无法添加它,则必须使用轮询来产生它的某些副作用,这在很大程度上是最后一种情况:
var timer = setInterval(function() {
if (/*...the side effect is present and so you know it's done..*/) {
clearInterval(timer);
timer = 0;
// The code that should run when it finishes
}
}, 100); // 100ms = ten times a second, adjust as appropriate
setTimeout(function() {
if (timer) {
// Give up
clearInterval(timer);
timer = 0;
}
}, 5000); // 5000ms = five seconds