简体   繁体   中英

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

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. Make a int or boolean (I used a int) and put the following in 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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