如何使用适用于android 7.1应用程序的ShortcutManager API创建动态应用程序快捷方式?

如何使用适用于android 7.1应用程序的ShortcutManager API创建动态应用程序快捷方式?,android,android-7.1-nougat,android-appshortcut,Android,Android 7.1 Nougat,Android Appshortcut,在安卓7.1中,开发者可以创建 我们可以通过两种方式创建快捷方式: 使用资源(XML)文件的静态快捷方式 使用ShortcutManagerAPI的动态快捷方式 那么如何使用ShortcutManager动态创建快捷方式呢?使用ShortcutManager,我们可以通过以下方式创建应用程序动态应用程序快捷方式: ShortcutManager shortcutManager; if (android.os.Build.VERSION.SDK_INT >= android.os.B

在安卓7.1中,开发者可以创建

我们可以通过两种方式创建快捷方式:

  • 使用资源(XML)文件的静态快捷方式
  • 使用
    ShortcutManager
    API的动态快捷方式

  • 那么如何使用
    ShortcutManager
    动态创建快捷方式呢?

    使用
    ShortcutManager
    ,我们可以通过以下方式创建应用程序动态应用程序快捷方式:

    ShortcutManager shortcutManager;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            shortcutManager = getSystemService(ShortcutManager.class);
            ShortcutInfo shortcut;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
                shortcut = new ShortcutInfo.Builder(this, "second_shortcut")
                        .setShortLabel(getString(R.string.str_shortcut_two))
                        .setLongLabel(getString(R.string.str_shortcut_two_desc))
                        .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                        .setIntent(new Intent(Intent.ACTION_VIEW,
                                Uri.parse("https://www.google.co.in")))
                        .build();
                shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
            }
    
    
        }
    
    字符串资源:

    <string name="str_shortcut_two">Shortcut 2</string>
    <string name="str_shortcut_two_desc">Shortcut using code</string>
    
    快捷方式2
    使用代码的快捷方式
    
    开发人员还可以使用
    ShortcutManager
    执行不同的任务:

    • 发布:用于重新定义整个动态快捷方式列表,或用于扩充现有的动态快捷方式列表
    • 更新:使用该方法
    • 删除:使用删除一组动态快捷方式,或使用删除所有动态快捷方式

    查看Github示例以了解更多信息


    检查并获取更多信息。

    我们可以使用ShortcutManager执行类似的意图操作

    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    
    ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "shortcut_web")
            .setShortLabel("catinean.com")
            .setLongLabel("Open catinean.com web site")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut))
            .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://catinean.com")))
            .build();
    
    shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));
    
        ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic")
            .setShortLabel("Dynamic")
            .setLongLabel("Open dynamic shortcut")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut_2))
            .setIntents(
                    new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                    })
            .build();
    shortcutManager.setDynamicShortcuts(Arrays.asList(dynamicShortcut, dynamicShortcut));
    
    我们可以使用ShortcutManager打开这样的活动

    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    
    ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "shortcut_web")
            .setShortLabel("catinean.com")
            .setLongLabel("Open catinean.com web site")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut))
            .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://catinean.com")))
            .build();
    
    shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));
    
        ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic")
            .setShortLabel("Dynamic")
            .setLongLabel("Open dynamic shortcut")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut_2))
            .setIntents(
                    new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                    })
            .build();
    shortcutManager.setDynamicShortcuts(Arrays.asList(dynamicShortcut, dynamicShortcut));
    
    适用于所有设备的完美解决方案(预/后Oreo设备)。 把这个放在你的电脑里

     public static void createShortcut(@NonNull Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
            Intent shortcutIntent = new Intent(activity, activityToOpen);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device 
                Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
                intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
                intent.putExtra("duplicate", false);
                Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
                activity.sendBroadcast(intent);
                System.out.println("added_to_homescreen");
            } else { 
                ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
                assert shortcutManager != null;
                if (shortcutManager.isRequestPinShortcutSupported()) {
                    ShortcutInfo pinShortcutInfo =
                            new ShortcutInfo.Builder(activity, "browser-shortcut-")
                                    .setIntent(shortcutIntent)
                                    .setIcon(Icon.createWithResource(activity, icon))
                                    .setShortLabel(title)
                                    .build();
    
                    shortcutManager.requestPinShortcut(pinShortcutInfo, null);
                    System.out.println("added_to_homescreen");
                } else {
                    System.out.println("failed_to_add");
                }
            }
        }
    
    public static void createShortcut(@NonNull Activity Activity,Class activityToOpen,String title,@DrawableRes int-icon){
    意向快捷内容=新意向(活动,活动打开);
    if(Build.VERSION.SDK_INT
    我们应该将快捷方式意图设置为目标活动,而不是提及隐含意图

    例如:

    my Manifest.xml中的活动是:

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
    
            </intent-filter>
        </activity>
    
    
    
    单击处理操作视图的动态快捷方式,我将登录到我的主活动。

    动态快捷方式: ShortcutManager用于管理所有动态快捷方式。ShortcutInfo表示单个快捷方式,然后通过ShortcutInfo.Builder将其添加到ShortcutManager

       //......................................* Java Dynamic Shortcuts
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    
    Intent thirdIntent = new Intent(this,ThirdActivity.class);
    thirdIntent.setAction(Intent.ACTION_VIEW);
    
    ShortcutInfo thirdScreenShortcut = new ShortcutInfo.Builder(this, "shortcut_third")
     .setShortLabel("Third Activity")
     .setLongLabel("This is long description for Third Activity")
     .setIcon(Icon.createWithResource(this, R.drawable.your_image))
     .setIntent(thirdIntent)
     .build();
     shortcutManager.setDynamicShortcuts(Collections.singletonList(thirdScreenShortcut));
    
    
    
    
    
    
    
    
      //......................................*  KOTLIN Dynamic Shortcuts
     val shortcutManager = getSystemService(ShortcutManager::class.java)
    
    
                val thirdIntent = Intent(this, ActivityThree::class.java)
                thirdIntent.action = Intent.ACTION_VIEW
    
                val dynamicShortcut = ShortcutInfo.Builder(this, "shortcut_third")
                    .setShortLabel("Third Activity")
                    .setLongLabel("This is long description for Third Activity")
                    .setIcon(Icon.createWithResource(this, R.drawable.your_image))
                    .setIntents(
                        arrayOf(
                            Intent(
                                Intent.ACTION_MAIN,
                                Uri.EMPTY,
                                this,
                                MainActivity::class.java
                            ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            thirdIntent
                        )
                    )
                    .build()
    
      shortcutManager!!.dynamicShortcuts = listOf(dynamicShortcut)
    
    主要注意事项:

    AppShortcuts简单项目:现在示例项目在这里了,M?API 25之后是否可用?它会抱怨Shortcutient没有操作如果您想创建活动的快捷方式,这个答案更好,只是一件事是您必须根据需要设置快捷方式意图,在我的例子中是:
    new intent(applicationContext,activity.class)
    。希望它能帮助那些正在寻找活动捷径的人