Issue
I am having issues with getting a URL to load in my webview that was clicked on in a email. It prompts to use my app which works fine and dandy, however its just loading a blank page. Here is my code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.util.Log;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "appdebug";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.getSettings().setJavaScriptEnabled(true);
Log.i(TAG, "webview working");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "overrideurl");
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
In Logcat its reporting the webview is working however i am not seeing anything from the overrideURl. Any help is much appreciated.
Solution
Here is the answer.
Uri uri = this.getIntent().getData();
try {
URL urls = new URL(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath()+ "?"+ uri.getQuery());
Log.i(TAG, String.valueOf(urls));
webView.loadUrl(String.valueOf(urls));
} catch (MalformedURLException e) {
e.printStackTrace();
}
The shouldOverrideUrlLoading
is, I believe used for opening links withing the webview
and is not looked at when the app is loaded from an url outside the app.
Answered By - megphn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.