Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void createShotcutUpperOreo(){
- if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)){
- final String PREF_FIRST_START = "AppFirstLaunch";
- SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
- if(settings.getBoolean(PREF_FIRST_START, true)){
- ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
- .setIntent(new Intent(this, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
- .setShortLabel("Test")
- .setIcon(IconCompat.createWithResource(this, R.drawable.ic_menu_camera))
- .build();
- boolean status = ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
- if (status){
- // record the fact that the app has been started at least once
- settings.edit().putBoolean(PREF_FIRST_START, false).apply();
- Toast.makeText(this, "Shortcut Created", Toast.LENGTH_SHORT).show();
- }
- }
- }
- else{
- Toast.makeText(this, "Not Created Shortcut", Toast.LENGTH_SHORT).show();
- // Shortcut is not supported by your launcher
- }
- }
- //need permission = <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
- //call it oncreate
- //for lower version
- public void createShortCut(){
- Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- shortcutintent.putExtra("duplicate", false);
- shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nav_shortcut");
- Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_menu_camera);
- shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
- shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
- sendBroadcast(shortcutintent);
- Toast.makeText(this, "Shortcut Created", Toast.LENGTH_SHORT).show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement