๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[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

 

๋Œ“๊ธ€