简体   繁体   中英

Android Studio - Webview - shouldOverrideUrlLoading - Do not open web browser

I developed a Webview app with Android Studio but when the user clicks a link from a web page in the WebView, Android is opening the default web browser. I already checked on Android Developers API Guides how to do but it does not work ;( on my app ... Guides Link: Handling Page Navigation

Who know how to use the "shouldOverrideUrlLoading() method" and how to insert it in my actual app?

Thanks

// MainActivity.Java

 package com.blabla.blabla; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.app.Application; import com.parse.Parse; import com.parse.ParseInstallation; public class MainActivity extends ActionBarActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.activity_main_webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.loadUrl("http://www.blablablablablabla.com/"); Parse.initialize(this, "ID Blablabla", "ID Blablabla"); ParseInstallation.getCurrentInstallation() .saveInBackground(); } } 

// activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <WebView android:id="@+id/activity_main_webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

// Android Manifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blabla.blabla" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:protectionLevel="signature" android:name="com.blabla.blabla.permission.C2D_MESSAGE" /> <uses-permission android:name="com.blabla.blabla.permission.C2D_MESSAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.parse.PushService" /> <receiver android:name="com.parse.ParseBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false"> <intent-filter> <action android:name="com.parse.push.intent.RECEIVE" /> <action android:name="com.parse.push.intent.DELETE" /> <action android:name="com.parse.push.intent.OPEN" /> </intent-filter> </receiver> <receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.blabla.blabla" /> </intent-filter> </receiver> </application> </manifest> 

I fixed it! I just added

 WebSettings webSettings = mWebView.getSettings(); 

and

 private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } 

To my MainActivity.java file... It is work great!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM