마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

버튼 리스너 안에서 바로가기 아이콘 생성 시 생성이 안되는 이유 [closed]

0 추천
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent action = new Intent(Intent.ACTION_VIEW).setClass(getApplicationContext(),MainActivity.class);

            ShortcutManager shortcutManager = getApplication().getSystemService(ShortcutManager.class);
            if (shortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), "TEST_APP")
                        .setIntent(action)
                        .setShortLabel("TEST_APP")
                        .setIcon(Icon.createWithResource(getApplicationContext(), R.mipmap.ic_launcher)).build(); //아이콘설정
                Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
                PendingIntent successCallback = PendingIntent.getBroadcast(getApplicationContext(), 0, pinnedShortcutCallbackIntent, 0);

                shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
            }
        }
        else {
            Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
            shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shortcutIntent.setClassName(getApplicationContext(), getClass().getName());
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

            Intent intent = new Intent();
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TEST_APP");
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));

            intent.putExtra("duplicate", false);
            intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

            sendBroadcast(intent);
        }

바로가기 아이콘을 생성하기 위한 코드입니다.

근데 이것을 일반적인 버튼 리스너 및 다이얼로그 버튼리스너 안에서 작성시 앱 아이콘 생성 이후

터치 시 애플리케이션이 설치되지않았습니다.

 

라는 문구가 나옵니다.

왜 리스너 안에서 사용을 하면 문제가 생기나요?

질문을 종료한 이유: 버튼 리스너를 인터페이스를 상속받아 따로 처리하여 해결
darkroyal (810 포인트) 님이 2020년 4월 20일 질문
darkroyal님이 2020년 4월 20일 closed
...