不使用浏览器打开网址

时间:2016-03-02 14:50:08

标签: android url web webview

我正在寻找一种不在浏览器中在应用上打开网址的方法。我发现了同样的问题here,有些人提到没有使用浏览器就无法从应用中打开网址。任何人都可以建议或显示一种不使用浏览器打开网址的方法。

任何帮助将不胜感激。提前谢谢。

3 个答案:

答案 0 :(得分:0)

您可以使用网络视图在您的应用中打开网址。这是关于它的官方google文档http://developer.android.com/reference/android/webkit/WebView.html

答案 1 :(得分:0)

您可以在新活动或片段中使用webview

如果你想要样品,请问我

package com.bala.computer.ojooo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class OjoooMain extends AppCompatActivity {

    WebView mWebView;
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ojooo_main);

        final ProgressBar Pbar;
        //final TextView txtview = (TextView)findViewById(R.id.tV1);
        Pbar = (ProgressBar) findViewById(R.id.pB1);

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

        mWebView = (WebView) findViewById(R.id.ojoooview);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://wad.ojooo.com/register.php?ref=legendbalaraja");
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                    Pbar.setVisibility(ProgressBar.VISIBLE);
                    //txtview.setVisibility(View.VISIBLE);
                }
                Pbar.setProgress(progress);
                if (progress == 100) {
                    Pbar.setVisibility(ProgressBar.GONE);
                    //txtview.setVisibility(View.GONE);
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_ojooo_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/ojoooview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp" />

    <ProgressBar
        android:id="@+id/pB1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:padding="2dip" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">

    </com.google.android.gms.ads.AdView>


</RelativeLayout>

从您的主要活动或活动中,您有按钮,将您的意图代码打开webview活动,并在按钮点击意图时传递网址

并在webview活动中以字符串形式获取并在加载网址

中使用它

答案 2 :(得分:0)

类 FileUrlViewer : AppCompatActivity() {

var webView: WebView? = null

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: 
                                   Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.file_url_viewer)
    val webView: WebView = 
           findViewById(R.id.fileUrlPreviewWV)

    webView.setVisible(true)

    clearCookies()
    
   CookieManager.getInstance().
     setAcceptCookie(true)
    webView.settings.builtInZoomControls = 
                                        true
    webView.settings.javaScriptEnabled = true
    webView.settings.domStorageEnabled = true
    webView.settings.loadWithOverviewMode 
                                      =true
    webView.settings.useWideViewPort = true
          
   webView.settings.setAppCacheEnabled(false)

    webView.setBackgroundColor(Color.TRANSPARENT)

    
    webView.webViewClient = object : WebViewClient() {

       var timeOut: Boolean = true

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            view?.loadUrl(url)
            return true
        }


        override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
            handler!!.proceed()
        }

        override fun onPageStarted(view: 
              WebView?, url: String?, favicon: 
                              Bitmap?) {
            super.onPageStarted(view, url, 
                                      favicon)


      thread{
         run(){
            timeOut = true
            try {
                Thread.sleep(60000)
            }catch (e: InterruptedException){
                e.printStackTrace()
            }
            if (timeOut) {
                
            Toast.makeText(this@FileUrlViewer, 
            " Oh No.... Time Out!!!",
            Toast.LENGTH_LONG).show()
            }
        }
    }.start()



        }

        override fun onPageFinished(view: 
               WebView?, url: String?) {
            timeOut = false
            view!!.setVisible(true)
        }

    }

    

    
 webView.loadUrl("https://www.google.co.in/")

}

}