如何在android oreo(8.0)上的主屏幕上添加应用启动器图标? [英] how to add app launcher icon to homescreen on android oreo(8.0)?

查看:158
本文介绍了如何在android oreo(8.0)上的主屏幕上添加应用启动器图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android 8.0上制作一个android应用.我已经阅读过使用静态快捷方式 但是该文档没有方法将启动器图标添加到主屏幕.

I am making an android app on Android 8.0. I have read develop doc of shortcut changed before at Using Static Shortcuts but this doc has not method add launcher icon to home screen.

我在8.0之前使用过方法.

I have used method before 8.0.:

Intent i= new Intent(Intent.ACTION_MAIN);  
i.addCategory(Intent.CATEGORY_LAUNCHER);

Intent intent = new Intent();     
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);     
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
context.getResources().getString(R.string.app_name));       
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
intent.setAction(com.android.launcher.action.INSTALL_SHORTCUT);
context.sendBroadcast(intent);

如何在主屏幕上添加应用午餐盒图标? 谢谢.

how to add app luncher icon to homescreen? thanks.

推荐答案

众所周知,从android 8.0版开始,许多行为发生了变化.尝试在oreo中使用以下代码在安装应用程序时自动创建一个快捷方式图标.

As we all know from android version 8.0 many behaviour changes. Try the following code in oreo to create a shortcut icon automatically at the time of install the application.

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {


            Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.class);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            shortcutIntent.setAction(Intent.ACTION_MAIN);

            ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getContext(), "shortcut")
                    .setShortLabel(getResources().getString(R.string.app_name))
                    .setIcon(IconCompat.createWithResource(getApplicationContext(), R.mipmap.ic_launcher))
                    .setIntent(shortcutIntent)
                    .build();
            ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcut, null);
        }

这篇关于如何在android oreo(8.0)上的主屏幕上添加应用启动器图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆