【问题标题】:How to programmatically create a shortcut of another app in Android?如何以编程方式在 Android 中创建另一个应用程序的快捷方式?
【发布时间】:2013-11-14 12:41:20
【问题描述】:

假设,我有一些 Android 应用程序可以帮助用户安装一些其他应用程序。有没有办法在主屏幕上创建这个应用程序的快捷方式?我也可以指定这些快捷键的位置吗?

【问题讨论】:

  • 我不明白;你说的快捷方式是什么?所以你想为已安装的应用程序创建应用程序图标/小部件?
  • @LotusUNSW 是的,我想为已安装的应用创建应用图标

标签: android


【解决方案1】:

试试这个:

public void createShortCut() {
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
    sendBroadcast(shortcutintent);
}

【讨论】:

  • 感谢代码,但有没有办法在创建新的捷径之前检查是否已经存在类似的捷径?
  • 试试shortcutintent.putExtra("duplicate", false);
  • 你在methodName后面漏掉了()(我加了)
【解决方案2】:

稍微改进的版本:我们正在检查快捷方式是否已经创建 如果用户将其从屏幕上移除,则不得创建

final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";

    @Override
    public void onStart(){
        super.onStart();

// Checking if ShortCut was already added
        sharedPreferences = getPreferences(MODE_PRIVATE);
        boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
        if (!shortCutWasAlreadyAdded) createShortcutIcon();


    } // end onStart


    // Creates shortcut on Android widget screen
    private void createShortcutIcon(){

        Intent shortcutIntent = new Intent(getApplicationContext(), Splash.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, 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);

        // Remembering that Shortcut was already added
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
        editor.commit();

        objPublicDelegate.showToast(getString(R.string.app_name)+ " shortcut created on screen.");

    } // end createShortcutIcon

【讨论】:

  • 谢谢。 ShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);如果没有这两行,有时应用程序会崩溃说“TransactionTooLargeException”。为避免此异常,必须添加这些行。
【解决方案3】:

Android 上没有这样的 API。

【讨论】:

  • 我应该评论而不是回答。
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多