How to detect shortcut in Home screen

前端 未结 3 505
借酒劲吻你
借酒劲吻你 2020-12-05 05:32

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.

相关标签:
3条回答
  • 2020-12-05 06:03

    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!

    0 讨论(0)
  • 2020-12-05 06:10

    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"/>    
    
    0 讨论(0)
  • 2020-12-05 06:16

    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);
    
    0 讨论(0)
提交回复
热议问题