상세 컨텐츠

본문 제목

Android 홈바로가기(Shortcut) 생성

Android

by 잘구운토스트 2018. 9. 17. 17:07

본문

홈바로가기 생성은 아래 코드 참고

 

사용법

1
2
3
4
5
6
 
String name = 바로가기의 이름 설정
int flag = 바로가기 실행 시 구분하기 위한 값 (필수가 아니므로 없어도 무방)
int resId = 바로가기 아이콘 설정
 
addShortcut(name, flag, resId);
cs

 

관련 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
    private static final String EXTRA_KEY_SHORTCUT = "extra_key_shortcut";
 
    /**
     * 바로가기 생성 (버전 체크)
     *
     * @param name 바로가기 이름
     * @param flag 바로가기 실행시 구분 값
     * @param resId 바로가기 아이콘
     */
    public void addShortcut(String name, int flag, @DrawableRes int resId) {
        Intent action= new Intent(context, StartActivity.class);
        action.putExtra(EXTRA_KEY_SHORTCUT, flag);    // 앱 내에서 바로가기 실행을 구분하기 위한 값
 
        // 디바이스의 현재버전 비교
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            sendShortcut(name, resId, action);        // 오레오 아래인 경우
        } else {
            sendShortcutOreo(name, resId, action);     // 오레오인 경우
        }
    }
 
    /**
     * 바로가기 생성 (오레오를 제외한 버전)
     *
     * @param name 바로가기 이름
     * @param resId 바로가기 아이콘
     * @param action 바로가기 인텐트 설정
     */
    private void sendShortcut(String name, @DrawableRes int resId, Intent action) {
        Intent intent = new Intent(ACTION_INSTALL_SHORTCUT);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getContext(), resId));
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, action);
        getContext().sendBroadcast(intent);
    }
 
    /**
     * 바로가기 생성 (오레오 버전)
     *
     * @param name 바로가기 이름
     * @param resId 바로가기 아이콘
     * @param action 바로가기 인텐트 설정
     */
    @TargetApi(Build.VERSION_CODES.O)
    private void sendShortcutOreo(String name, @DrawableRes int resId, Intent action) {
        ShortcutManager shortcutManager = getContext().getSystemService(ShortcutManager.class);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(getContext(), name) // 여러종류를 생성할 경우 구분하기 위한 값으로 꼭 name으로 안해도 됨
                    .setIntent(action)
                    .setShortLabel(name)
                    .setIcon(Icon.createWithResource(getContext(), resId)).build();
            Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
            PendingIntent successCallback = PendingIntent.getBroadcast(getContext(), 0, pinnedShortcutCallbackIntent, 0);
 
            shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
        }
    }
cs

 

'Android' 카테고리의 다른 글

Android UI Open source  (0) 2019.05.31
Android Status bar 접기(올리기)  (0) 2018.09.19
Android APP 재시작 방지  (0) 2018.09.17
android App 알림 허용 상태 값 가져오기  (0) 2018.02.20
Android Log 표시(Log 짤림 현상 개선)  (0) 2017.12.04

관련글 더보기

댓글 영역