簡體   English   中英

等待功能結束

[英]Wait for a function to end

我有以下代碼:

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文件(在我的網站上有一個Webview)調用了該函數,我無法使用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;
                }
            }
        });
    }

displayGPSRequest將需要提供一種知道何時完成的方法。 如果當前不是,則需要對其進行編輯以添加一個(或使用輪詢,這不是一個好主意)。

通常的方法是:

  1. 通過承諾。

  2. 通過原始回調。

因此,如果displayGPSRequest返回一個promise(或您將其編輯為):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM