我创建了一个基于url的WebView应用程序,例如website.some,当我们通过我的webview应用程序打开该链接时,我共享了一个帖子链接,比如website.some / post_name /到我的朋友,不是直接打开帖子页面链接,它只打开我的网站主页链接(wesite.some)。我希望像我的webview应用程序中的Snapdeal或flipkart直接打开页面这样的操作。请帮帮我。
像这样的主要活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
wv = (WebView) findViewById(R.id.webView1);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setAppCacheEnabled(false);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setSupportMultipleWindows(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setDisplayZoomControls(false);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
settings.setDomStorageEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
settings.setUseWideViewPort(true);
settings.setSavePassword(true);
settings.setSaveFormData(true);
wv.loadUrl("http://website.some/");
wv.setWebViewClient(new MywebViewClient());
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private class MywebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("website.some/")){
return false;
}
return false;
}
ProgressDialog pd = null;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("please wait...");
pd.setMessage("Page is loading..");
pd.show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction()== KeyEvent.ACTION_DOWN){
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
if(wv.canGoBack()){
wv.goBack();
}
else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
试试这个。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("website.some")){
view.loadUrl(url); //optional
return false;
}
return true;
}
答案 1 :(得分:0)
当onPageFinished内的url被更改时调用的shouldOverrideUrlLoading方法。
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView viewx, String url)
{
if (url.contains("websiteLink"))
view.loadUrl(url);
return false;
}