Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. private void createShotcutUpperOreo(){
  2. if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)){
  3. final String PREF_FIRST_START = "AppFirstLaunch";
  4. SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
  5.  
  6. if(settings.getBoolean(PREF_FIRST_START, true)){
  7. ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
  8. .setIntent(new Intent(this, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
  9. .setShortLabel("Test")
  10. .setIcon(IconCompat.createWithResource(this, R.drawable.ic_menu_camera))
  11. .build();
  12. boolean status = ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
  13. if (status){
  14. // record the fact that the app has been started at least once
  15. settings.edit().putBoolean(PREF_FIRST_START, false).apply();
  16. Toast.makeText(this, "Shortcut Created", Toast.LENGTH_SHORT).show();
  17. }
  18. }
  19. }
  20. else{
  21. Toast.makeText(this, "Not Created Shortcut", Toast.LENGTH_SHORT).show();
  22. // Shortcut is not supported by your launcher
  23. }
  24. }
  25.  
  26. //need permission = <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  27.  
  28. //call it oncreate
  29.  
  30. //for lower version
  31.  
  32. public void createShortCut(){
  33. Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  34. shortcutintent.putExtra("duplicate", false);
  35. shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nav_shortcut");
  36. Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_menu_camera);
  37. shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
  38. shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
  39. sendBroadcast(shortcutintent);
  40. Toast.makeText(this, "Shortcut Created", Toast.LENGTH_SHORT).show();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement