首页 > 解决方案 > 尝试在 Android Oreo 及更高版本上向我的应用添加启动器快捷方式时出现问题

问题描述

我正在开发一个简单的应用程序,它有一个按钮,可以在启动器上创建我应用程序主要活动的快捷方式。

我关注了以下主题: 如何在安装应用程序时将应用程序的快捷方式添加到主屏幕?

但是,当我单击按钮并执行“createShortcut”功能时,没有创建快捷方式。我正在使用 android Oreo 和 on 以及运行 Android 8.1 的 Pixel 2 模拟器在 Galaxy S7 上测试我的应用程序。

我想你能帮我解决这个问题。这是主要活动:

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>

标签: androidshortcutlauncher

解决方案


经过一番研究,我发现在 Android Oreo 及以上版本中创建快捷方式的方法有点不同。我遵循了 Android Authority 的本教程:https ://www.androidauthority.com/android-nougat-oreo-static-dynamic-pinned-shortcuts-845686/

谢天谢地,它奏效了!我希望它也对你有用


推荐阅读