如何在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);
}