[안드로이드스튜디오_문화][App Shortcut]

기말 지하기포·2024년 1월 3일
0

#Shortcut Android Developer 공식문서
=>https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts?hl=ko

-App Shortcut 즉, 바로가기 기능은 총 3가지 방법으로 분류된다. [1.정적 바로가기] , [2.동적 바로가기] , [3.고정된 바로가기] 이렇게 3가지의 방법으로 분류된다.

-[1.dynamic 바로가기] : 사용자가 원하는 화면으로 이동 할 수 있는 바로가기

-[2.static 바로가기] : 바로가기 목록이 고정되어 있다.

-[3.pinned 바로가기] : 특정 화면으로 이동하는 앱 런처를 만든다.

dynamic AppShortcut

-dynamic 바로가기는 앱이 실행되는 동안에도 사용자가 원한다면 바로가기를 설정 할 수 있게 도와준다.

-pushDynamicShortcut을 활용해서 바로가기를 등록 할 수 있고, removeDynamicShortcut을 활용해서 등록한 바로가기를 해제 할 수 도 있다.

-동적 바로가기를 추가하는 방법을 적용한 예시코드이다.


private fun addDynamicShortcut() {
        val shortcut = ShortcutInfoCompat.Builder(applicationContext,"dynamic")
            .setShortLabel("짧은 라벨")
            .setLongLabel("긴 라벨")
            .setIcon(IconCompat.createWithResource(
                applicationContext, R.drawable.ic_launcher_background
            ))
            .setIntent(
                Intent(applicationContext, MainActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                    putExtra("shortcut_id" , "dynamic")
                }
            )
            .build()
        ShortcutManagerCompat.pushDynamicShortcut(applicationContext , shortcut)
    }
  • shortcut 변수에 ShortcutInfoCompat 객체가 저장되며 , 이 객체는 동적 바로가기를 나타내고 라벨 , 아이콘 , 인텐트 등의 정보가 포함되어 있다.
  • setShortLabel(String) : 바로가기 아이콘의 짧은 라벨 설정
  • setLongLabel(String) : 바로가기 아이콘의 긴 라벨 설정
  • setIcon(icon) : 바로가기 아이콘을 생성
  • setIntent(Intent 객체) : 바로가기 아이콘을 클릭했을 때 실행 될 인텐트 설정
  • .build() : 위에서 설정된 값들을 바탕으로 dynamic AppShortcut 생성
  • ShortcutManagerCompat.pushDynamicShortcut(applicationContext, ShortcutInfoCompat 객체) : 앱에서 생성된 dynamic AppShortcut을 구글 어시스턴트로 보내서 어시스턴트가 사용자에게 실제로 dynamic AppShortcut을 표시 할 수 있도록 한다.

-위 코드는 동적 바로가기를 생성하는 고정된 코드이다. 단지 바뀌는 것은 라벨의 이름 , 아이콘의 종류 , 이동하는 Activity 이들 뿐이다.

static AppShortcut

-static AppShortcut은 앱의 바로가기를 앱에 내장시켜서 고정적으로 제공해주며 최대 4개까지만 가능하다. 이를 통해 앱은 사용자에게 일관되는 앱의 바로가기 기능을 제공해준다.

-코드가 정적이기 때문에 코드를 실행한 적이 없더라도 이미 앱에 통합되어 있어야 하므로 XML 파일로 static AppShortcut과 관련된 내용들을 정의해야한다.

1.res/xml/ 폴더 내부에 새로운 xml 파일 만들기

-새로운 xml 파일을 만들 때 FileName과 RootElement 둘 다 에 shortcuts를 넣어줘야 한다.

-xml 파일에 아래코드(공식문서에서 제공)을 붙혀넣은 후 내 앱에 알맞게 코드를 수정해주면 된다.

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="바로가기 아이디 설정"
    android:enabled="활성 또는 비활성화 설정(Boolean)"
    android:icon="정적 바로가기 아이콘 설정"
    android:shortcutShortLabel="짧은 라벨 이름 설정"
    android:shortcutLongLabel="긴 라벨 이름 설정"
    android:shortcutDisabledMessage="바로가기 비활성화 시 표시될 메시지">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="내 앱의 패키지 이름"
      android:targetClass="바로가기 누르면 이동할 Activity 이름" >
    <extra
		android:name="putExtra 사용 시 name"
		android:value="putExtra 사용 시 value"/>
    </intent>    
    <categories android:name="android.shortcut.conversation" />
    <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
  </shortcut>
</shortcuts>

2.AndroidManifest 설정

-AndroidManifest에 들어가서 이동할 activity 태그 내부에 meta-data 태그를 만들어서 아래와 같이 작성해줘야 한다.

<meta-data android:resource="@xml/shortcuts"
                android:name="android.app.shortcuts" />
  • 이렇게 함으로서 1번에서 생성한 xml 파일과 Activity를 연결 할 수 있다. 따라서 정적 바로가기가 맨 첨부터 표시 될 수 있는 것이다.

pinned AppShortcut

-API26 이상[오!레!오!]에서는 고정된 바로가기를 만들 수 있다. 정적 및 동적 바로가기와 달리 고정된 바로가기는 지원되는 런처에 별도의 아이콘으로 표시된다.

-isRequestPinShourtcutSupported()를 사용해서 휴대폰이 고정 바로가기를 지원하는지 확인해야 한다.

-고정된 바로가기 요청의 결과를 처리하고 성공적으로 고정된 바로가기가 추가되면 알림을 받을 수 있도록 PendingIntent를 활용 할 수 있다.



-pinned AppShortcut 또한 바로가기를 만드는 전형적인 빌더가 존재한다. 전형적인 빌더에 대한 예시코든 아래에 있고 해당 코드에 대한 설명을 참고로 학습하면 된다.

private fun addPinnedShortcut() {
	> API26 보다 API 레벨이 작으면 걍 return 내고 끝낸다.
	if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
		return
	}
	val shortcutManager = getSystemService<ShortcutManager>()!!

	val shortcut = ShortcutInfo.Builder(applicationContext , "pinned")
            .setShortLabel("고정된 짧은 라벨")
            .setLongLabel("고정된 긴 라벨")
            .setIcon(Icon.createWithResource(
                applicationContext , R.drawable.ic_launcher_background
            ))
            .setIntent(
                Intent(applicationContext, MainActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                    putExtra("shortcut_id" , "pinned")
                }
            )
            .build()
	val callbackIntent = shortcutManager.createShortcutResultIntent(shortcut)
	val successPendingIntent = PendingIntent.getBroadcast(
            applicationContext,
            0,
            callbackIntent,
            PendingIntent.FLAG_IMMUTABLE
	)
	shortcutManager.requestPinShortcut(shortcut ,successPendingIntent.intentSender)
}
  • getSystemServiceShortcutManager!! : 반환되는 서비스 타입이 ShortcutManager 클래스의 인스턴스임을 알려준다.

  • ShortcutInfo.Builder() 는 dynamic AppShortCut에서 설명한 것과 동일하다.

  • shortcutManager.createShortcutResultIntent(shortcut) : 고정된 바로가기의 추가에 대한 결과를 받기 위한 콜백 인텐트 생성

  • PendingIntent.getBroadcast() : 고정된 바로가기 추가가 성공시 실행될 PendingIntent 생성

  • shortcutManager.requestPinShortcut(shortcut, successPendingIntent.intentSender) : 사용자의 휴대폰의 홈 화면에 고정된 바로가기를 고정하는 것을 요청하고 만약 내가 수락하면 successPendingIntent가 실행된다.



실제 수행 영상

-

profile
포기하지 말기

0개의 댓글