Activity에서 다른 Activity를 호출할 때 Intent를 이용하여 화면전환을 하는 것과 같이 핸드폰에 기본적으로 장착된 App의 Activity를 호출 하는 경우에는 Intent의 action 중 ACTION_VIEW를 이용하여 웹 브라우저를 호출하는 방법 입니다.
기본 브라우저를 이용하여 웹 페이지 URL을 연결하려면 public Intent (String action, Uri uri) 메소드를 사용하는데 action 부분에는 ACTION_VIEW를 입력하고 Uri는 웹 주소를 입력하면 됩니다.

 

Intent intentUrl = new Intent(Intent.ACTION_VIEW, Uri.parse("https://copycoding.tistory.com/"));
startActivity(intentUrl);

 

URL에 파라미터를 추가해서 넘기려면 URL 뒤에 붙여주도록 합니다.

 

String param = "/47";
Intent intentParam = new Intent(Intent.ACTION_VIEW, Uri.parse("https://copycoding.tistory.com/" + param));
startActivity(intentParam);

 

인터넷을 사용해야 하므로 Manifest에 permission을 추가해 주면 됩니다.
<uses-permission android:name="android.permission.INTERNET" />

 

버튼을 누르면 첫화면으로 이동 하고 param을 붙인경우 페이지를 찾아 갑니다.

 

 

전체 소스
수정된 파일은 3개 입니다.

AndroidManifest.xml

 

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ActionViewUri">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="URL"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.229"
        android:onClick="clickUrl"/>

    <Button
        android:id="@+id/btnParam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="176dp"
        android:text="URL+Param"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnUrl"
        app:layout_constraintVertical_bias="0.0"
        android:onClick="clickParam"/>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

MainActivity.java

 

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

   @Override protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
   }
     
  public void clickUrl(View view) {
     Intent intentUrl = new Intent(Intent.ACTION_VIEW, Uri.parse("https://copycoding.tistory.com/"));
     startActivity(intentUrl);
  }

  public void clickParam(View view) {
      String param = "/47";
      Intent intentParam = new Intent(Intent.ACTION_VIEW, Uri.parse("https://copycoding.tistory.com/" + param));
      startActivity(intentParam);
   }
}

 

- copy coding -


+ Recent posts