如何在Android中以编程方式添加应用程序快捷方式

use*_*269 12 android

我需要将我的Android应用程序添加到主屏幕作为快捷程序.

请给出这个想法.如果可能,请告诉我如何管理现有快捷方式(删除和添加更多快捷方式).

Par*_*ani 9

我读过一篇文章,可以帮助您在主屏幕上以编程方式添加应用程序快捷方式.

你可以参考这个例子.

您还可以在此处参考与快捷方式相关的stackoverflow问题.


Vir*_*hme 5

在第一个屏幕的onCreate()方法中调用此方法.还要确保像我一样使用SharedPreferences检查应用是否第一次运行:

 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, this.getResources().getString(R.string.app_name));
    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);
}

    // TO check app is installed first time.
    SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE);
    if(!prefs.getBoolean("isFirstTime", false)){
        addShortcut();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("isFirstTime", true);
        editor.commit();
    } 
Run Code Online (Sandbox Code Playgroud)