2017년 8월 15일 화요일

Unity, android 특정 앱의 설치 여부 확인(AndroidJavaClass사용)

안드로이드 환경에서 특정 앱의 설치 여부를 패키지 이름으로 확인합니다.


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AndroidUtil : MonoBehaviour 
{
    #if UNITY_ANDROID && !UNITY_EDITOR 

    public static bool IsAppInstalled(string bundleID)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");

        AndroidJavaObject launchIntent = null;

        //if the app is installed, no errors. Else, doesn't get past next line
        try
        {
            launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleID);
        }
        catch (Exception ex)
        {
            Debug.Log("exception" + ex.Message);
        }

        return (launchIntent == null ? false : true);
    }

    #endif
}



아래처럼 확인 가능
bool existApp = AndroidUtil.IsAppInstalled("com.android.otherApp");


댓글 없음:

댓글 쓰기