how to add app launcher icon to homescreen on android oreo(8.0)?

淺唱寂寞╮ 提交于 2020-01-24 11:15:34

问题


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.

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.


回答1:


As mentioned on Android Developers

The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.

Check out this link Android 8.0 Behavior Changes




回答2:


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);
        }


来源:https://stackoverflow.com/questions/46842984/how-to-add-app-launcher-icon-to-homescreen-on-android-oreo8-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!