Android create shortcuts on the home screen

前端 未结 10 737
忘掉有多难
忘掉有多难 2020-11-22 13:16

What I want to do is:

1) I\'m inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an

相关标签:
10条回答
  • 2020-11-22 13:21

    I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.

    private void ShortcutIcon(){
    
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }
    

    Don't forget to change your activity name, icon resource & permission.

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    

    Happy coding !!!

    Edit:

    For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.

    addIntent.putExtra("duplicate", false);
    

    Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.

    0 讨论(0)
  • 2020-11-22 13:26

    The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.

    The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:

    <activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
      <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </activity>
    

    Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

    // create shortcut if requested
    ShortcutIconResource icon =
        Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
    
    Intent intent = new Intent();
    
    Intent launchIntent = new Intent(this,ActivityToLaunch.class);
    
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    
    setResult(RESULT_OK, intent);
    
    0 讨论(0)
  • 2020-11-22 13:26

    To add a shortcut to the home screen use this code.

    public void addShortcutToHomeScreen(Context context) {
            if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
                ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                        .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                        .setShortLabel("Label Goes Here")
                        .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher_shortcut))
                        .build();
                ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
            } else {
                Toast.makeText(context, R.string.no_shortcut, Toast.LENGTH_SHORT).show();
            }
        }
    

    and no extra permission need !!!

    0 讨论(0)
  • 2020-11-22 13:27
    final Intent shortcutIntent = new Intent(this, SomeActivity.class);
    
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    
    0 讨论(0)
  • 2020-11-22 13:29

    Since API level 26, using com.android.launcher.action.INSTALL_SHORTCUT is deprecated. The new way of creating shortcuts are using ShortcutManager.

    It mentions that there are 3 kinds of shortcuts, static, dynamic and pinned. Based on your requirements, you can choose to create them.

    0 讨论(0)
  • 2020-11-22 13:32

    @Siddiq Abu Bakkar Answer works. But in order to prevent creating shortcut every time app launches use shared Preferences.

    final String PREF_FIRST_START = "AppFirstLaunch";
     SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
        if(settings.getBoolean("AppFirstLaunch", true)){
    
            // record the fact that the app has been started at least once
            settings.edit().putBoolean("AppFirstLaunch", false).commit();
            ShortcutIcon();
    
        }
    
    0 讨论(0)
提交回复
热议问题