2016-10-19 76 views
2

我使用WebView在我使用這些設置Android應用程序:Android:如何在Android 4.1中使用JavaScript加載webview?

webview = (WebView) findViewById(R.id.webViewViewer); 
    WebSettings webSettings = webview.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
    webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
    webview.getSettings().setAppCacheEnabled(true); 
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    webSettings.setDomStorageEnabled(true); 
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); 
    webSettings.setUseWideViewPort(true); 
    webSettings.setSavePassword(true); 
    webSettings.setSaveFormData(true); 
    webSettings.setEnableSmoothTransition(true); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.getSettings().setUseWideViewPort(true); 
    webview.setWebChromeClient(new WebChromeClient()); 

我也是在做一些方法的javascript editation onPageFinished()。

當我的Android 5.0使用它,一切正常,但是當我的Android 4.1測試控制檯顯示error: Unsafe JavaScript attempt to access frame with URL http://www.example.sk

我應該怎麼做,使其在較舊的Android版本的工作太?

回答

0
// Actually, 4.1 has issues on javascript. If you want to load web view on 4.1 .you can android web view by using this code 

    public class MainActivity extends AppCompatActivity { 

     private final String url = "Your Url"; 
    private WebView mWebView; 
     private CustomTabsClient mClient; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if (AppStatus.getInstance(this).isOnline(this)) { 
       int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
       if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP) { 
        setContentView(R.layout.noraml); 
        mWebView = (WebView) findViewById(R.id.webView); 
        mWebView.getSettings().setJavaScriptEnabled(true); 

        mWebView.setWebViewClient(new WebViewClient() { 
         @Override 
         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
    //this is for avoiding web page not found 
          view.loadUrl("file:///android_asset/index.html"); 
          Toast.makeText(getBaseContext(), "Oops!!! Please check your internet connection", Toast.LENGTH_SHORT).show(); 

         } 
        }); 

        mWebView.loadUrl(url); 
       } else { 

        setContentView(R.layout.activity_main); 
        CustomTabsServiceConnection mConnection = new CustomTabsServiceConnection() { 

         @Override 
         public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) { 
          mClient = customTabsClient; 
         } 

         @Override 
         public void onServiceDisconnected(ComponentName componentName) { 
          mClient = null; 
         } 
        }; 

        String packageName = "com.android.chrome"; 
        CustomTabsClient.bindCustomTabsService(this, packageName, mConnection); 
       } 
      } 
      else { 
       setContentView(R.layout.error); 
      } 
     } 
     public void loadCustomTabs(View view) { 


       CustomTabsIntent.Builder mBuilder = new CustomTabsIntent.Builder(getSession()); 

       mBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.indigo_500)); 
       mBuilder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), 
         R.mipmap.ic_arrow_back_white_24dp)); 
       mBuilder.addMenuItem("Share", setMenuItem()); 
       mBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left); 
       mBuilder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right); 
       CustomTabsIntent mIntent = mBuilder.build(); 
       mIntent.launchUrl(this, Uri.parse(url)); 


     } 

     private CustomTabsSession getSession() { 
      return mClient.newSession(new CustomTabsCallback() { 
       @Override 
       public void onNavigationEvent(int navigationEvent, Bundle extras) { 
        super.onNavigationEvent(navigationEvent, extras); 
       } 
      }); 
     } 

     private PendingIntent setMenuItem() { 
      Intent shareIntent = new Intent(Intent.ACTION_SEND); 
      shareIntent.setType("text/plain"); 
      shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject For you website"); 
      shareIntent.putExtra(Intent.EXTRA_TEXT, url); 
      return PendingIntent.getActivity(this, 0, shareIntent, 0); 
     } 
} 
//Custom web view 
public class CustomWebView extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_custom_web_view); 
     class MyWebViewClient extends WebViewClient { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (Uri.parse(url).getHost().equals(getIntent().getStringExtra("url"))) 
        return false; 
       else 
        return super.shouldOverrideUrlLoading(view, url); 
      } 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       view.loadUrl("file:///android_asset/index.html"); 
       Toast.makeText(getBaseContext(), "Oops!!! Please check your internet connection", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     WebView webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 

     webView.loadUrl(getIntent().getStringExtra("url")); 
     webView.setWebViewClient(new MyWebViewClient()); 
    } 
} 
相關問題