Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class MainActivity extends AppCompatActivity {
- private WebView web;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- web = (WebView) findViewById(R.id.web);
- web.setWebViewClient(new WebViewClient());
- web.loadUrl("https://site.com");
- web.getSettings().setJavaScriptEnabled(true);
- }
- @Override
- public void onBackPressed() {
- if (web.canGoBack()) {
- web.goBack();
- } else {
- super.onBackPressed();
- }
- }
- }
- // В этой переменной у нас хранится адрес сайта
- String site = "http://site.com/";
- Uri siteUri = Uri.parse(site);
- // Создаём интент
- Intent openLinkIntent = new Intent(Intent.ACTION_VIEW, siteUri);
- // Открываем в браузере
- startActivity(openLinkIntent);
- private class MyWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- 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;
- }
- }
- WebView myWebView = (WebView) findViewById(R.id.webview);
- myWebView.setWebViewClient(new MyWebViewClient());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement