← 글 목록
0%

Android 7.1 앱 바로가기 구현하기

Android 7.1에는 앱 아이콘을 길게 눌러 특정 기능으로 바로 이동하는 앱 바로가기(Shortcut)가 추가되었다. 매니페스트와 XML 리소스로 정적 바로가기를 구성하는 방법을 살펴본다.

픽셀런처의 홈 화면에서 아이콘을 길게 누르면 앱 바로가기 목록이 펼쳐지며, 이동을 위해 움직이게 되는경우 비활성화 되는 방식이다. 애플 iPhone의 3D터치와 기능은 같으나 작동 방법은 소프트웨어로 구현되었다는 점에서 다르다.

구현 방법은 아주 간단하다. 구글에서 정해둔 규약을 잘 따라 매니페스트에 메타데이터를 정의하면 된다. 에버노트의 경우 검색 바로가기, 노트 바로 작성하기 등 앱에 진입하지 않고 빠르게 특정한 작업을 할 만한 사항들의 기능을 넣었다.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
             package="com.example.myapplication">
  <application ... >
    <activity android:name="Main">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="android.app.shortcuts"
                 android:resource="@xml/shortcuts" />
    </activity>
  </application>
</manifest>

매니페스트의 메타데이터에 앱 바로가기 리소스를 추가한 뒤, XML에 바로가기 항목을 정의한다. 커스텀 스킴이 있다면 intent에 해당 스킴을 지정하고, 그렇지 않다면 실행할 Activity 클래스 이름을 지정한다.

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
  </shortcut>
  
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
  </shortcut>

</shortcuts>


동적 구현 방법

xml을 통해 정적으로 구현할 수도 있으나 상황에 따라 앱 바로가기 아이템을 추가하거나 삭제 하고 싶은 경우 ShortcutManager를 통해서 추가하거나 삭제 또는 업데이트할 수 있다.

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("PlayStore")
    .setLongLabel("Open PlayStore App")
    .setIcon(Icon.createWithResource(context, R.drawable.icon))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("market://")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

앱 바로가기 기능을 통해 앱에 진입 하지 않도 빠르게 검색을 한다거나 재생목록을 재생하는등 다양하게 활용하여 사용편의성을 높일 수 있다. 이미 이런 사용성을 생각한 개발자라면 런처 바로가기 기능을 통해 이미 구현을 했을 것이고 앱 바로가기 기능은 쉽게 구현할 수 있을 것이라 생각된다.

참고: https://developer.android.com/preview/shortcuts.html

X 공유