setAllowUniversalAccessFromFileURLs?

10,647

The reason it is crashing is that you might not running on an device which has API Level 16 or Up.

From the Official Site HERE you can See that Method getAllowUniversalAccessFromFileURLs is Added After API Level 16. So you will

public abstract boolean getAllowUniversalAccessFromFileURLs ()

Added in API level 16 Gets whether JavaScript running in the context of a file scheme URL can access content from any origin. This includes access to content from other file scheme URLs.

Returns whether JavaScript running in the context of a file scheme URL can access content from any origin See Also setAllowUniversalAccessFromFileURLs(boolean)

So To Access this you Should Write Below Code :

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
Share:
10,647
crickpatel0024
Author by

crickpatel0024

Updated on June 04, 2022

Comments

  • crickpatel0024
    crickpatel0024 almost 2 years

    my problem is i run the android app and display error log given below.how to solve it .

    public class SeoShopTest extends Activity {
    
        WebView myWebView;
        TextView mDisplay;
        AsyncTask<Void, Void, Void> mRegisterTask;
        String Message;
        String deviceId;
        String regId;
        IntentFilter gcmFilter;
        SharedPreferences sharedPref;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            checkNotNull(SERVER_URL, "https://android.googleapis.com/gcm/send");
            checkNotNull(SENDER_ID, "201519466560");
    
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);
            setContentView(R.layout.activity_seo_shop_test);
    
            myWebView = (WebView) findViewById(R.id.web);
    
            gcmFilter = new IntentFilter();
            gcmFilter.addAction("GCM_RECEIVED_ACTION");
    
            sharedPref = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            String CheckTokenId = sharedPref.getString(
                    getResources().getString(R.string.token_id), "");
            String CheckShopId = sharedPref.getString("shop_id", "");
    
            Log.v("log", " check TokenID" + CheckTokenId + "  check Shop id "
                    + CheckShopId);
    
            if ((!CheckTokenId.equals("")) && (!CheckShopId.equals(""))) {
                Log.v("log",
                        "  lhl hklkh lkh ll  : : :  shopId and TokenId Get IT ");
                Intent i = new Intent(SeoShopTest.this, devicephp.class);
                i.putExtra("tokenId", CheckTokenId);
                i.putExtra("shop_id", CheckShopId);
                i.putExtra("app_var", "1");
                startActivity(i);
            }
    
    
            final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(
                    this);
            myWebView.addJavascriptInterface(myJavaScriptInterface,
                    "AndroidFunction");
    
            // mDisplay = (TextView) findViewById(R.id.display);
            registerReceiver(mHandleMessageReceiver, new IntentFilter(
                    DISPLAY_MESSAGE_ACTION));
            regId = GCMRegistrar.getRegistrationId(SeoShopTest.this);
    
            WebSettings settings = myWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setDatabaseEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setAllowFileAccess(true);
            settings.setBuiltInZoomControls(true);
            settings.setUseWideViewPort(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setLoadWithOverviewMode(true);
            myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            //settings.setAllowUniversalAccessFromFileURLs(true);
    
             myWebView.setWebViewClient(new WebViewClient() {  
                   public boolean shouldOverrideUrlLoading(WebView view, String url)  {                  
    
                                //handle stuff here 
                                //e.g. view.loadUrl(url);   
                       Log.v("log"," on ovverRide " + url);
                                return true;    
                            }
    
                            public void onPageFinished(WebView view, String url) {
                                //dismiss the indeterminate progress dialog
                                Log.v("log", "onPageFinished: " + url);
                                myWebView.setEnabled(false);
    
                            } 
                        });
    
            myWebView.loadUrl("file:///android_asset/www/index.html");
    
            if (regId.equals("")) {
                // Automatically registers application on startup.
                Log.v("log", " sender ID" + SENDER_ID);
                GCMRegistrar.register(this, SENDER_ID);
    
                regId = GCMRegistrar.getRegistrationId(SeoShopTest.this);
    
                Log.v("log", " re register main if " + regId);
            } else {
                Log.v("log", " re register main else " + regId);
    
                // Device is already registered on GCM, needs to check if it is
                // registered on our server as well.
                if (GCMRegistrar.isRegisteredOnServer(this)) {
                    // Skips registration.
                    // mDisplay.append("already_registered" + "\n");
                } else {
                    // Try to register again, but not in the UI thread.
                    // It's also necessary to cancel the thread onDestroy(),
                    // hence the use of AsyncTask instead of a raw thread.
                    final Context context = this;
                    mRegisterTask = new AsyncTask<Void, Void, Void>() {
    
                        @Override
                        protected Void doInBackground(Void... params) {
                            boolean registered = ServerUtilities.register(context,
                                    regId);
    
                            if (!registered) {
                                GCMRegistrar.unregister(context);
                            }
                            return null;
                        }
    
                        @Override
                        protected void onPostExecute(Void result) {
                            mRegisterTask = null;
                        }
    
                    };
                    mRegisterTask.execute(null, null, null);
                }
            }
    
        }
    
        private void SavePreferences(String key, String value) {
            sharedPref = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString(key, value);
            editor.commit();
        }
    
        public class JavaScriptInterface {
            Context mContext;
    
            JavaScriptInterface(Context c) {
                mContext = c;
            }
    
    
            public void showToast(String shopId) {
    
                sharedPref = PreferenceManager
                        .getDefaultSharedPreferences(getApplicationContext());
                String token_new_ID = sharedPref.getString(getResources()
                        .getString(R.string.token_id), "");
    
                Log.v("log", "tokenNEWID " + token_new_ID + " shoipdi  " + shopId);
    
                if ((!shopId.equals("")) && (token_new_ID.equals(""))) {
    
                    SavePreferences("shop_id", shopId);
    
                    Intent i = new Intent(SeoShopTest.this, LoginCompleteUrl.class);
                    i.putExtra("shopId", shopId);
                    startActivity(i);
                } else if ((!token_new_ID.equals("")) && (!shopId.equals(""))) {
    
                    Log.v("log", "shop id else if " + shopId);
                    SavePreferences("shop_id", shopId);
                    Intent i = new Intent(SeoShopTest.this, devicephp.class);
                    i.putExtra("tokenId", token_new_ID);
                    i.putExtra("shop_id", shopId);
                    startActivity(i);
                } else {
                    Log.v("log", " else token  " + token_new_ID);
    
                }
    
            }
    
            public void MainUrl(String url) {
                Log.v("log"," login main url " + url);
    
                String url_new = "http://"+url;
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url_new));
                startActivity(i);
    
            }
    
    
        }
    
        @Override
        protected void onDestroy() {
            if (mRegisterTask != null) {
                mRegisterTask.cancel(true);
            }
            unregisterReceiver(mHandleMessageReceiver);
            GCMRegistrar.onDestroy(this);
            super.onDestroy();
        }
    
        private void checkNotNull(Object reference, String name) {
            if (reference == null) {
                throw new NullPointerException(
                        "Please set the %1$s constant and recompile the app.");
            }
        }
    
        private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // String newMessage = intent.getExtras().getString("message");
                String GcmMessage = intent.getExtras().getString("message");
                int GcmId = intent.getExtras().getInt("id");
                String GCmType = intent.getExtras().getString("type");
    
                Log.v("log", " new Message from server GsmMessage " + GcmMessage
                        + " id" + GcmId + "type " + GCmType);
    
                // mDisplay.append(newMessage + "\n");
            }
        };
    
    }
    

    and my error is given below:

    02-12 10:37:14.629: ERROR/AndroidRuntime(330): FATAL EXCEPTION: main
        02-12 10:37:14.629: ERROR/AndroidRuntime(330): java.lang.NoSuchMethodError: android.webkit.WebSettings.setAllowUniversalAccessFromFileURLs
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.example.seoshoptest.SeoShopTest.onCreate(SeoShopTest.java:93)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.os.Handler.dispatchMessage(Handler.java:99)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.os.Looper.loop(Looper.java:123)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.main(ActivityThread.java:4627)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at java.lang.reflect.Method.invokeNative(Native Method)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at java.lang.reflect.Method.invoke(Method.java:521)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at dalvik.system.NativeStart.main(Native Method)