λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’š 개발/μ•ˆλ“œλ‘œμ΄λ“œ

[Android] 앱이 μ—†μœΌλ©΄ μ•±μŠ€ν† μ–΄λ‘œ 이동, 앱이 있으면 ν•΄λ‹Ή μ•± μ—΄κΈ°

by devshin.kr 2023. 8. 9.
728x90
λ°˜μ‘ν˜•

 
λ‹€μŒ λ™μž‘μ„ ν•˜λŠ” κΈ°λŠ₯을 κ΅¬ν˜„ν•΄ 보겠닀.

 

(0. μΉ΄μΉ΄μ˜€ν†‘μ˜ νŒ¨ν‚€μ§€λͺ…을 μ•ŒκΈ° μœ„ν•΄ APKPURE μ—μ„œ νŒ¨ν‚€μ§€λͺ…을 νšλ“ν–ˆλ‹€)
https://apkpure.com

Download APK on Android with Free Online APK Downloader - APKPure

V2-X: Fast & Secure VPN Enjoy the ultimate privacy , security and internet filter breaker V2-X VPN

apkpure.com

 


 1. MainActivity.java

package com.devshin93.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Context ctx = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = getApplicationContext();

        Button btn = (Button) findViewById(R.id.test);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startApp();
            }
        });
    }


    private void startApp(){
        // apkName : app νŒ¨ν‚€μ§€λͺ…
        String apkName = "com.kakao.talk";
        // PackageManager : ν˜„μž¬ μž₯μΉ˜μ— μ„€μΉ˜λœ μ‘μš©ν”„λ‘œκ·Έλž¨ νŒ¨ν‚€μ§€μ™€ κ΄€λ ¨λœ λ‹€μ–‘ν•œ μ’…λ₯˜μ˜ 정보λ₯Ό κ²€μƒ‰ν•˜λŠ” 클래슀
        PackageManager pm = ctx.getPackageManager();
        // 앱이 μ„€μΉ˜λ˜μ–΄ μžˆλŠ”μ§€ 확인
        boolean appInstalled = false;
        PackageInfo info = null;

        try {
            // getPackageInfo(String packageName, int flags) : μ‹œμŠ€ν…œμ— μ„€μΉ˜ 된 μ• ν”Œλ¦¬μΊμ΄μ…˜ νŒ¨ν‚€μ§€μ— λŒ€ν•œ 전체 정보λ₯Ό 검색
            // PackageManager.GET_ACTIVITIES : PackageInfo ν”Œλž˜κ·Έ: νŒ¨ν‚€μ§€ ν™œλ™μ— λŒ€ν•œ 정보λ₯Ό λ°˜ν™˜
            // ν•΄λ‹Ή 앱이 μ„€μΉ˜λ˜μ–΄ μžˆλŠ”μ§€ 확인
            info = pm.getPackageInfo(apkName, PackageManager.GET_ACTIVITIES);
            // μ•± μ„€μΉ˜ ν”Œλž˜κ·Έ 값을 true 둜 μ„€μ •
            appInstalled = true;
        } catch(PackageManager.NameNotFoundException e) {
            // PackageManager.NameNotFoundException : μ§€μ •λœ νŒ¨ν‚€μ§€λ₯Ό μ‹œμŠ€ν…œμ—μ„œ 찾을 수 μ—†κ±°λ‚˜ ν˜ΈμΆœμžκ°€ μ†ŒμŠ€ λ˜λŠ” λŒ€μƒ νŒ¨ν‚€μ§€μ— λŒ€ν•œ μ„ΈλΆ€ 정보λ₯Ό 쿼리할 수 μ—†λŠ” 경우 λ°œμƒν•˜λŠ” exception 처리
            appInstalled = false;
        }

        // 앱이 μ„€μΉ˜λ˜μ–΄ 있으면
        if(appInstalled) {
            // getLaunchIntentForPackage(String packageName)
            // : front-door μ•‘ν‹°λΉ„ν‹°μ—μ„œ μ‹€ν–‰ν•  νŒ¨ν‚€μ§€μ˜ intentλ₯Ό λ°˜ν™˜. νŒ¨ν‚€μ§€λ₯Ό 찾지 λͺ» ν•˜λ©΄ null을 λ°˜ν™˜ν•œλ‹€.
            Intent intent = pm.getLaunchIntentForPackage(apkName);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (ctx != null)
                // Activity λŠ” μ•± μ•ˆμ˜ 단일 화면을 λ‚˜νƒ€λƒ„. Activity 의 μƒˆ μΈμŠ€ν„΄μŠ€λ₯Ό μ‹œμž‘ν•˜λ €λ©΄ Intent λ₯Ό startActivity() 둜 μ „λ‹¬ν•˜λ©΄ λœλ‹€.
                ctx.startActivity(intent);
        }
        // 앱이 μ„€μΉ˜λ˜μ–΄ μžˆμ§€ μ•ŠμœΌλ©΄
        else {
            // Uri.parse(String uriString) : 주어진 μΈμ½”λ”©λœ URI λ¬Έμžμ—΄μ„ ꡬ문 λΆ„μ„ν•˜λŠ” Uriλ₯Ό λ§Œλ“¦
            Uri marektUri = Uri.parse("market://details?id=" + apkName);
            // Intent.ACTION_VIEW : μ•‘ν‹°λΉ„ν‹°κ°€ μ‚¬μš©μžμ—κ²Œ ν‘œμ‹œν•  수 μžˆλŠ” μ–΄λ–€ 정보λ₯Ό 가지고 μžˆμ„ λ•Œ startActivity() κ°€ μžˆλŠ” μΈν…νŠΈμ—μ„œ μ‚¬μš©
            // 예λ₯Ό λ“€μ–΄ 가러리 μ•±μ—μ„œ λ³Ό μ‚¬μ§„μ΄λ‚˜ 지도 μ•±μ—μ„œ λ³Ό μ£Όμ†Œ 등이 이에 ν•΄λ‹Ή
            Intent intent = new Intent(Intent.ACTION_VIEW, marektUri);
            // Intent.FLAG_ACTIVITY_NEW_TASK : μ„€μ •λœ 경우, 이 ν™œλ™μ€ 이 기둝 μŠ€νƒμ—μ„œ μƒˆ μž‘μ—…μ˜ μ‹œμž‘μ΄ 됨
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (ctx != null)
                ctx.startActivity(intent);
        }
    }
}

 


2. activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start App"
        />
    
</RelativeLayout>

 ν…ŒμŠ€νŠΈν•  λ²„νŠΌ μΆ”κ°€!


 
3. 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.devshin93.myapplication">
    
    <!-- μ—¬κΈ°μ—¬κΈ° -->
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
    
    <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.StartApp">
        <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>

μœ„μ˜ κΆŒν•œ(permission) μΆ”κ°€!
 
 
μ°Έκ³  : 
PackageManager κ΄€λ ¨ : https://developer.android.com/reference/android/content/pm/PackageManager

PackageManager  |  Android Developers

developer.android.com

 
Intent κ΄€λ ¨ : https://developer.android.com/guide/components/intents-filters

μΈν…νŠΈ 및 μΈν…νŠΈ ν•„ν„°  |  Android 개발자  |  Android Developers

An Intent is a messaging object you can use to request an action from another app component . Although intents facilitate communication between components in several ways, there are three fundamental use cases: An Activity represents a single screen in…

developer.android.com

 
Intent Flag κ΄€λ ¨ : https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK

Intent  |  Android Developers

developer.android.com

 

λ°˜μ‘ν˜•

λŒ“κΈ€