[안드로이드] URI Scheme을 이용한 호출

변준영·2023년 2월 16일
0

# URI Scheme이란 : 이전글 참고

URI Scheme 이용한 App 호출

  • URI Scheme을 이용하여 앱과 웹에서 A라는 app을 각각 어떻게 호출할 지 알아본다.

1. A앱 URI Scheme 등록 [1]

  • 다른 앱에서 호출할 수 있도록 URI scheme을 등록
  • 각 Activity마다 Intent-filter를 등록함으로써 URI Scheme를 생성
  • Activity의 URI Scheme는 {shcheme}://{host}

A app의 AndroidMenifest.xml

<intent-filter>
	<action android:name="android.intent.action.MAIN" />
	<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
	<action android:name="android.intent.action.VIEW" />
		<category android:name="android.intent.category.DEFAULT" />
		<category android:name="android.intent.category.BROWSABLE" />
      	<!-- host : 이동할 페이지(액티비티)
             scheme : 앱의 이름 -->
  		<data
			android:host="byunhost"
			android:scheme="byunscheme" />
</intent-filter>

2. 앱에서의 A app 호출

  • 호출할 앱(B app)에서 위에서 등록한 Scheme 정보를 통해 A app을 호출
  • 웹 URI을 호출하는 방법과 똑같이 사용
  btnAapp.setOnClickListener {
		String uriScheme = "byunscheme://byunhost";
		Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(uriScheme));
        startActivity(intent);
  }

3. 웹에서의 A app 호출[2],[3]

  • javascript에서 다음과 같은 코드 호출
"intent://{host}#Intent;scheme={scheme};package={package};end";
<html>`
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta name="viewport" content="width=device-width">
  </head>
  <body>
      <a href ="callScheme()">call scheme</a>
  </body>
</html>
<script>
	function callScheme(){
    	location.href = "intent://byunhost#Intent;scheme=byunscheme;action=android.intent.action.VIEW;category=android.intent.category.BROWSABLE;package=com.example.myapplication;end";
	}
</script>

참고

[1] https://velog.io/@jeep_chief_14/URL-Scheme-0xm9upiv
[2] https://gomest.tistory.com/7
[3] https://helloit.tistory.com/366

0개의 댓글