如何在我的Android应用程序中打开URL链接

时间:2016-10-30 17:27:11

标签: java android

我使用两个按钮打开两个网址链接。当我按下按钮它进入浏览器。但我想在我的应用程序中打开这些url链接。有人可以帮我解决我的问题。我的代码如下:

活动主要:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:orientation="vertical"
tools:context="com.example.vkumar.myapplication.MainActivity"
android:background="#d3a7a7">
<Button
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:text="GOOGLE"
    android:onClick="browser1"
    android:id="@+id/browser1"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:background="@android:color/holo_blue_dark" />
<Button
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:text="MAIL"
    android:id="@+id/browser2"
    android:onClick="browser2"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:background="@android:color/holo_blue_dark" />    
</LinearLayout>

主要活动:

package com.example.vkumar.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity  {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button browser1 = (Button) findViewById(R.id.browser1);
    Button browser2 = (Button) findViewById(R.id.browser2);
    Button browser3 = (Button) findViewById(R.id.browser3);
    Button browser4 = (Button) findViewById(R.id.browser4);
    Button browser5 = (Button) findViewById(R.id.browser5);
}
public void browser1 (View view) {
Intent intent = new         Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.co.in/"));
    startActivity(intent);
}
public void browser2 (View v) {
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.gmail.com/"));
    startActivity(intent);
}
}

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.vkumar.myapplication">

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

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="MY APP" android:supportsRtl="true" android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>
</manifest>

1 个答案:

答案 0 :(得分:1)

简单地将url传递给webView按钮单击然后在应用程序内显示。见下面的例子。

On Button链接将网址传递给webview活动

Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("URL", "https://www.google.co.in/");
startActivity(intent);



public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        String url = getIntent().getStringExtra("URL");
        webView.loadUrl(url );

    }

}