如何确定是否存在主屏幕快捷方式



有没有办法确定特定的主屏幕快捷方式是否存在?

我的应用程序在设备的主屏幕上安装快捷方式在特定条件下的启动时间,我不想重复要显示的快捷方式。我也不希望Toast消息显示每次设备引导。我发现了一个名为EXTRA_SHORCUT_DUPLICATE,它将防止正在安装,但启动器仍然显示"快捷方式已经存在"Toast消息。我宁愿不依赖这个未记录的消息Intent Extra(如果有支持的技术)。

这不是侵入性的吗?为什么不只添加一次,让用户决定是否保留它?

当您的应用程序创建快捷方式时,将布尔值设置为"true"并将其存储在存储器中(例如小文件或共享引用)。当您的程序尝试创建快捷方式的时候,请检查其值。

**// Checking if ShortCut was already added
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean("PREF_KEY_SHORTCUT_ADDED", false);
        if (shortCutWasAlreadyAdded) return;
        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, "SBM");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(addIntent);
        // Remembering that ShortCut was already added
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("PREF_KEY_SHORTCUT_ADDED", true);
        editor.commit();**

最新更新