[英]How to determine if a home screen shortcut exists?
Is there a way to determine whether a particular home screen shortcut exists? 有没有办法确定是否存在特定的主屏幕快捷方式?
My application installs a shortcut on the home screen at device startup time under certain conditions and I don't want duplicate shortcuts to appear. 在某些情况下,我的应用程序会在设备启动时在主屏幕上安装快捷方式,并且我不希望出现重复的快捷方式。 I also don't want Toast messages appearing saying "Shortcut created" or "Shortcut already exists" every single time the device boots.
我也不希望每次启动设备时都出现Toast消息,说“创建快捷方式”或“快捷方式已存在”。 I've found an undocumented Intent Extra called EXTRA_SHORTCUT_DUPLICATE which will prevent duplicate shortcuts from being installed but the Launcher still displays the "Shortcut already exists" Toast message.
我发现了一个未记录的Intent Extra,名为EXTRA_SHORTCUT_DUPLICATE,它将防止安装重复的快捷方式,但启动器仍会显示“快捷方式已存在” Toast消息。 I'd rather not rely on this undocumented Intent Extra if there is a supported technique for this.
如果有受支持的技术,我宁愿不要依赖未公开的Intent Extra。
当您的应用创建快捷方式时,请将布尔值设置为“ true”,然后将其存储在存储中(例如小文件或sharedpreferences)。并在您尝试创建快捷方式时检查其值。
isn't that kind of intrusive? 那不是一种侵入吗? Why not just add it once, and let the user decide if they want to keep it or not?
为什么不只添加一次,然后让用户决定是否要保留它呢?
**// 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();**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.