简体   繁体   English

如何在主屏幕中检测快捷方式

[英]How to detect shortcut in Home screen

I have an app that allows you to create "shortcuts" in Home Screen. 我有一个应用程序,允许您在主屏幕中创建“快捷方式”。 But can i detect if the "shortcuts" already exist, i didn't have to create it again. 但我可以检测到“快捷方式”是否已经存在,我不必再次创建它。 Thanks. 谢谢。

I was having the same issue. 我遇到了同样的问题。 I don't think its possible for you to tell if it's there, but I think what I used will work for you as well. 我不认为它可能会告诉你它是否存在,但我认为我使用的也适合你。

Simply uninstall the shortcut before you add it! 只需在添加快捷方式之前卸载快捷方式!

intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);

Just make sure you add the following to your manifest file. 只需确保将以下内容添加到清单文件中即可。

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>    

I do not know whether it is possible to detect the shortcut or not, but instead of uninstalling and then installing, you can use the Extra field as 我不知道是否可以检测到快捷方式,但是不是卸载然后安装,而是可以使用Extra字段作为

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
...
intent.putExtra("duplicate", false);
...
sendBroadcast(intent);

I think I found a good way to make sure the shortcut is not add more than once and also the Toast message to go away. 我想我找到了一个很好的方法来确保快捷方式不会多次添加,并且Toast消息也会消失。 Make a int or boolean (I used a int) and put the following in onResume: 创建一个int或boolean(我用了一个int)并将以下内容放在onResume中:

if(shortcut != 1) //or shortcut == true
{
    ...(shortcut code)...
    shortcut = 1;
    savePref("shortcut", shortcut); //overriding method to save int's or booleans
}

If user delete's the user data then it will add another shortcut to the home screen but I can live with that since most users don't delete that kind of stuff anyway unless they are uninstalling the app. 如果用户删除用户数据,那么它将添加另一个快捷方式到主屏幕,但我可以忍受,因为大多数用户无论如何都不删除那种东西,除非他们正在卸载应用程序。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM