패키지명을 받아서 앱을 띄워주어야 할 때가 있다.
사용자의 휴대폰에 해당 앱이 깔려있다면 실행하고, 깔려있지 않고 Google Play는 이용할 수 있다면 구글 플레이의 앱 상세 페이지로, 만약에 그렇지도 않다면 구글 플레이 웹에서 앱 상세 페이지로 이동 시키는 함수이다.
try{
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch (Exception e){
e.printStackTrace();
String url = "market://details?id=" + packageName;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
하지만 안드로이드 11을 타겟팅한다고 하시면 메니페스트에서 하나를 추가해주어야 한다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="내 프로젝트 패키지명">
<!-- !!여기!! -->
<queries>
<package android:name="실행하고싶은 외부앱 패키지명" />
</queries>
<!-- !!추가!! -->
<application
/>
이렇게
<queries 태그안쪽에 패키지 적어서 추가해줘야 외부 앱을 실행시킬 수 있다.
'Android > Android 기초' 카테고리의 다른 글
[Android] 안드로이드 4대 컴포넌트(2) - 서비스(Service) (0) | 2023.09.03 |
---|---|
[Android] 화면 회전시 액티비티(Activity)에서 데이터는 어떻게 저장해야할까? (0) | 2023.09.01 |
[Android] 안드로이드 4대 컴포넌트(1) - Activity (0) | 2023.09.01 |
[Android] 안드로이드 구조와 동작 원리(2) - 구성 요소 및 빌드 과정 (0) | 2023.08.29 |
[Android] 안드로이드 구조와 동작원리 (1) - Android 플랫폼은 어떻게 생겼나? (0) | 2023.08.29 |