Ярлык главного экрана не добавит - PullRequest
0 голосов
/ 16 января 2019

Я пытаюсь программно добавить короткое замыкание на домашний экран, но оно не работает.Код работает для Oreo и выше, но не ниже этого.

public void addShortcutToHomeScreen() {

        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {

            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
                    .setIntent(new Intent(this, ActivityMemes.class).setAction(Intent.ACTION_MAIN)) //!!! intent's action must be set on oreo
                    .setShortLabel("Memeify")
                    .setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);

        } else {

            Intent shortcutIntent = new Intent(getApplicationContext(),
                    Activity.class);

            shortcutIntent.setAction(Intent.ACTION_MAIN);

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

            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            addIntent.putExtra("duplicate", false);
            getApplicationContext().sendBroadcast(addIntent);

        }
    }

Вот мой файл манифеста.

<activity
            android:name="com.my.package.Activities.Activity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.CREATE_SHORTCUT" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>

Пожалуйста, скажите мне, что я делаю неправильно, потому что я пыталсяисправить это и не удалось.И есть ли лучший способ, чем этот?Спасибо за любую помощь, спасибо.

РЕДАКТИРОВАТЬ:

Нашел это в Logcat.

2019-01-16 13:27:46.773 735-13377/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } from com.chameleon.memeify (pid=14824, uid=10300) requires com.android.launcher.permission.INSTALL_SHORTCUT due to receiver com.sec.android.app.launcher/com.android.launcher3.home.InstallShortcutReceiver

1 Ответ

0 голосов
/ 16 января 2019

вы можете использовать этот метод в вашем классе приложений

public void createShortCut() {

            Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            shortcutintent.putExtra("duplicate", false);
            shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
            Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.logo);
            shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
            shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
            sendBroadcast(shortcutintent);

        }
    }

и применить это разрешение

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...