설치시 숏컷 만들기

작성자

카테고리:

// Note that a shortcut is created automagically if the app is installed via Play store.
// Change "APP_NAME" by your app name. *MrObvious*

/*Manifest file - add this */
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

/* MainActivity.java */
public class MainActivity ... {
    ...
    private SharedPreferences appSettings;
    ...

    protected void onCreate(Bundle savedInstanceState) {
        ...
        ...
        appSettings = getSharedPreferences("APP_NAME", MODE_PRIVATE);
        // Make sure you only run addShortcut() once, not to create duplicate shortcuts.
        if(!appSettings.getBoolean("shortcut", false)) {
            addShortcut();
        }
    }

    private void addShortcut() {
        //Adding shortcut for MainActivity
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "APP_NAME");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.putExtra("duplicate", false);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);

        SharedPreferences.Editor prefEditor = appSettings.edit();
        prefEditor.putBoolean("shortcut", true);
        prefEditor.commit();
    }
}