Проблемы при попытке добавить ярлык запуска в мое приложение на Android Oreo и выше - PullRequest
0 голосов
/ 12 апреля 2020

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

Я подписался на следующую тему: Как добавить ярлык приложения на домашний экран при установке приложения?

Однако, когда я нажимаю кнопку и выполняю команду " createShortcut ", ярлык не создается. Я тестирую свое приложение на Galaxy S7 с android Oreo и включенным и эмулятором Pixel 2 с Android 8.1.

Я бы хотел помочь вам в решении этой проблемы. Это основное действие:

package com.example.test_shortcut_application;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }



    public void createShortcut(View v){
        System.out.println("trying to add a shortcut to homescreen");
        Intent intentShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher_background);
        intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
        intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
        intentShortcut.putExtra("duplicate", false);
        sendBroadcast(intentShortcut);

    }



}

Это основное действие xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="createShortcut"
        android:text="Add Shorcut"
        tools:layout_editor_absoluteX="214dp"
        tools:layout_editor_absoluteY="215dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Это файл манифеста приложения:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_shortcut_application">
    <uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 Ответ

0 голосов
/ 13 апреля 2020

После небольшого исследования я обнаружил, что в Android Oreo и выше метод создания ярлыка немного отличается. Я следовал этому руководству от Android Автор: https://www.androidauthority.com/android-nougat-oreo-static-dynamic-pinned-shortcuts-845686/

К счастью, это сработало! Я sh это будет работать и для вас

...