2

I am trying to read local storage data from javascript using WebView in android.

Here is my code

 @Override
    public void onPageFinished(WebView view, String url) {
      getLocalStorageAndSaveOnAndroidPref("androidDeliveryAppStickyNotificationTitle", view);
    }


 public static void getLocalStorageAndSaveOnAndroidPref(String key, WebView view) {
        String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
        Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: function called");
        view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + key + "')"), value -> {
            Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: "+value);
            SharePref.setDataPref(key, value);
        });
    }

But this is giving me null when it's loaded the first time.

I want to achieve that the local storage data should be loaded the first time the page is finished.

Any idea how to achieve that.

Manish
  • 170
  • 9

2 Answers2

2

I have better solution for this, it will work for the first time opening WebView. Try this.

myWebView.post(() -> {
            String[] keys = {"androidDeliveryAppStickyNotificationTitle","androidDeliveryAppStickyNotificationSubtitle","androidAppNewSoundAlertTitle"};
            getLocalStorageAndSaveOnAndroidPref(keys, myWebView, () -> {
                if (SharePref.getBooleanFromPref(Constant.HAS_SOCKET)) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        RestartServiceBroadcastReceiver.scheduleJob(context);
                    } else {
                        ProcessMainClass bck = new ProcessMainClass();
                        bck.launchService(context);
                    }
                }
            });

        });
public static void getLocalStorageAndSaveOnAndroidPref(String[] keys, WebView view,dataSavedCallback savedCallback) {
        for(int i=0;i<=keys.length-1;i++){
            String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
            Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: function called");
            int finalI = i;
            view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
                    SharePref.setDataPref(keys[finalI], value);
                    if(finalI==keys.length-1){
                        savedCallback.onDataSaved();
                    }
                }
            });

        }

    }
2

I have a better solution for this it will work for the first time opening WebView.

you just have to use a custom callback. like this

public interface DataSavedCallback {
  void onDataSaved();
}
  public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
WebView view,dataSavedCallback savedCallback) {
  
    String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
     view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
                }
            });

    }

then use this function to get the value of the key from the webview local storage

public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
WebView view,dataSavedCallback savedCallback) {
  
    String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
     view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
                }
            });

    }

you can call the above function anywhere using your webview

myWebView.post(() -> {
            String[] keys = {"androidDeliveryAppStickyNotificationTitle","androidDeliveryAppStickyNotificationSubtitle","androidAppNewSoundAlertTitle"};
            getLocalStorageAndSaveOnAndroidPref(keys, myWebView, new dataSavedCallback() {
                @Override
                public void onDataSaved() {
                //do whatever you want
                }
            });

        });

Jagadish S
  • 121
  • 4