티스토리 뷰

728x90

핵심 코드

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)

 

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


설명

앱에서 웹페이지를 띄우는 방법에는 대표적으로 웹뷰가 있어요.

하지만 반드시 외부 브라우저를 실행시켜 해당 페이지를 띄워야 하는 경우가 있기도 하지요.

대표적으로 앱 다운로드 유도를 위한 구글 플레이 스토어 페이지로의 이동이 있겠네요.

변수 url 에 여러분께서 띄우고자 하는 웹페이지의 주소를 넣으시면 됩니다.


시연 동영상

화면 중앙의 My Application 이라고 적힌 text view 를 터치하면 외부 브라우저가 실행되는 앱을 제작해 보았습니다.

참고로 제 단말기의 기본 브라우저는 웨일입니다.

외부 브라우저로 웨일이 실행될 것입니다.

 


전체 코드

1. MainActivity.kt

package com.example.myapplication

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.tv).setOnClickListener {
            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)
        }
    }
}

 

2. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/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"?>
<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">

    <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/Java] 외부 브라우저 실행하는 방법

 

[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); 설명 앱..

boywin1992.tistory.com


728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함