I’m attempting the second challenge at the end of chapter 30, so I modify the WebViewClient to override shouldOverrideUrlLoading: I log the call, then if the scheme is http or https, return false, otherwise create an intent, call startActivity, and return true.
My problem starts immediately, when I try to log the call before actually doing anything. When I click on an http/https link, no problem, I get a log message. But clicking on the “Install” button gives me nothing.
Now, if I don’t modify the WebViewClient, I get the expected error message about “market://…” when I click “Install”, so the button is clickable in the WebView.
Am I missing something obvious here?
Thanks.
StartActivity crashes in the following code. If the scheme is “http” or “https”, everything works fine, but when I click a link whose scheme is “market”, application crashes. The following code is called from public boolean shouldOverrideUrlLoading(WebView view, String url)
if (uri.getScheme().equals("http") || uri.getScheme().equals("https")) {
return false;
} else {
final Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
return true;
}
BC2017
December 4, 2017, 2:17pm
3
Hi, I face problems when I work with this challenge. After sometimes, I found the solution in stackoverflow
I would like to share it with you:
PhotoPageFragment.java
package com.bignerdranch.android.photogallery;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/**
* Created by behin on 12/3/2017.
*/
public class PhotoPageFragment extends VisibleFragment {
private static final String ARG_URI = "photo_page_url";
private Uri mUri;
private WebView mWebView;
private ProgressBar mProgressBar;
public static PhotoPageFragment newInstance(Uri uri) {
Bundle args = new Bundle();
args.putParcelable(ARG_URI, uri);
PhotoPageFragment fragment = new PhotoPageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUri = getArguments().getParcelable(ARG_URI);
}
@Nullable
@Override
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_page, container, false);
mProgressBar = (ProgressBar) v.findViewById(R.id.progress_bar);
mProgressBar.setMax(100); // WebChromeClient reports in range 0-100
mWebView = (WebView) v.findViewById(R.id.web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView webView, int newProgress) {
if (newProgress == 100) {
mProgressBar.setVisibility(View.GONE);
} else {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(newProgress);
}
}
public void onReceivedTitle(WebView webView, String title) {
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.getSupportActionBar().setSubtitle(title);
}
});
mWebView.setWebViewClient( new photoPageWebViewClient());
mWebView.loadUrl(mUri.toString());
return v;
}
private class photoPageWebViewClient extends WebViewClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String uriString = request.getUrl().toString();
// is this a play store URL?
String partialUrl = "/store/apps/details?id=";
if (uriString.contains(partialUrl)) {
// extract the app id from the URL
int pos = uriString.indexOf(partialUrl) + partialUrl.length();
String appId = uriString.substring(pos);
try {
// open the google play app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appId));
startActivity(intent);
return true; // we overrode the url load
} catch (ActivityNotFoundException e) {
// no google play app, load URL in device browser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + appId));
startActivity(intent);
return true;
}
}
return false; // no override, let the webview load this url
}
}
public boolean webViewCanGoBack() {
return mWebView != null && mWebView.canGoBack();
}
public void webViewGoBack() {
if (mWebView != null) {
mWebView.goBack();
}
}
}
Good-luck!