[Android/Java] 외부 브라우저 실행하는 방법
핵심 코드
String url = "https://play.google.com/store/apps/details?id=net.daum.android.tistoryapp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
<uses-permission android:name="android.permission.INTERNET" />
설명
앱에서 웹페이지를 띄우는 방법에는 대표적으로 웹뷰가 있어요.
하지만 반드시 외부 브라우저를 실행시켜 해당 페이지를 띄워야 하는 경우가 있기도 하지요.
대표적으로 앱 다운로드 유도를 위한 구글 플레이 스토어 페이지로의 이동이 있겠네요.
변수 url 에 여러분께서 띄우고자 하는 웹페이지의 주소를 넣으시면 됩니다.
시연 동영상
화면 중앙의 My Application 이라고 적힌 text view 를 터치하면 외부 브라우저가 실행되는 앱을 제작해 보았습니다.
참고로 제 단말기의 기본 브라우저는 웨일입니다.
외부 브라우저로 웨일이 실행될 것입니다.
전체 코드
1. MainActivity.java
package com.example.myjavaapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.tv).setOnClickListener(view -> {
String url = "https://play.google.com/store/apps/details?id=net.daum.android.tistoryapp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
});
}
}
2. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
http://schemas.android.com/apk/res/android"</manifest xmlns:android="
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
http://schemas.android.com/apk/res/android"</androidx.constraintlayout.widget.constraintlayout xmlns: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">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
코틀린을 사용하시는 분은 아래 게시글을 읽어보아요.
2022.07.28 - [android] - [Android/Kotlin] 외부 브라우저 실행하는 방법
[Android/Kotlin] 외부 브라우저 실행하는 방법
핵심 코드 val url = "https://play.google.com/store/apps/details?id=net.daum.android.tistoryapp" val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) 설명 앱에서 웹페이지를..
boywin1992.tistory.com