首页 文章

如何在android中打开url

提问于
浏览
-2

Please read my question first

我想在我的应用中打开一个不在手机浏览器中的URL .

您可以在Facebook或Twitter上看到他们如何在自己的应用程序中打开链接 .

This will redirect to my phone's browser

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

但我想像Facebook App一样打开网址 .

这种视图是否有任何特定的API .

5 回答

  • -1

    我想你想用Chrome Custom Tabs,查看链接!

    这里是网站的基本示例

    // Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
    // Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
    // and launch the desired Url with CustomTabsIntent.launchUrl()
    
    String url = ¨https://paul.kinlan.me/¨;
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));
    
  • 1

    以下代码将帮助您在应用中打开网址:

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    
    public class AppWebViewActivity extends Activity {
    
        private WebView mWebview;
        private String mWebViewUrl;
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            mWebview  = new WebView(this);
            mWebViewUrl= getIntent().getStringExtra("web_view_url");
    
            mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    
            final Activity activity = this;
    
            mWebview.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                }
            });
    
            mWebview .loadUrl(mWebViewUrl);
            setContentView(mWebview );
    
        }
    
    }
    

    在清单中定义上述活动,如下所示:

    <activity
            android:name="<package-name>.AppWebViewActivity "
            android:screenOrientation="portrait" >
        </activity>
    

    并添加互联网权限:

    <uses-permission android:name="android.permission.INTERNET" />
    

    要调用此活动,请按照以下代码:

    Intent intent = new Intent(getBaseContext(), AppWebViewActivity .class);
    intent.putExtra("web_view_url", "your-complete-url");
    startActivity(intent)
    

    您可以使用自定义ui,在这种情况下,您必须在xml中定义webview并在webview活动中引用布局 .

    快乐的编码!!!!

  • 2

    您可以在应用程序中通过webview打开网址,如下所示:

    webview下面的xml代码;

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.whiskey.servicedog.DocumentPlay"
        tools:showIn="@layout/activity_document_play">
    
        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true">
    
        </WebView>
    
    </RelativeLayout>
    

    下面的java文件代码如下:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_document_play);
    
            webView = (WebView) findViewById(R.id.webView);
    
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.loadUrl(urlpath);
        }
    

    希望这可以帮助你

  • -1

    您可以在新活动中使用webview打开网址 . https://developer.android.com/guide/webapps/webview.html

  • 1

    添加webview活动/片段,并在webview中显示网站 .

相关问题