안드로이드

[안드로이드] shortcut의 구현

IT꿈나무 2022. 8. 31. 18:56
반응형

목적:

 안드로이드의 쑛컷을 구현한다.

 앱을 롱클릭했을때 바로가기가 보인다.

 %shortcut.xml은 난독화에서 제외 시킬것.

 

구현방법:

  AndroidManifest.xml

<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>

res/xml/shortcuts.xml

<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" >
            <extra android:name="extra_key" android:value="extra_value" /> //인자 전달
        </intent>
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
    <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

MainActivity.java

        Intent intent = getIntent();
        String extra_key = intent.getStringExtra("extra_key");
        MyLog.i(TAG,"extra_key: "+extra_key);

참고자료:

[1] 바로가기 만들기, https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts?hl=ko 

[2] 기능스미카,  https://developer.android.com/guide/app-actions/action-schema?hl=ko#capability_schema

반응형