Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create app icon on home screen on app installation in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 284
    Comment on it

    Here is how we can create app shortcut icon on device's home screen after app installation:

    There's only a few lines of code.

    if(!MyUtility.getIsFirstTime(splash).equals("true"))
            {
                MyUtility.setIsFirstTime(splash, "true");
                Intent shortcutIntent = new Intent();
                shortcutIntent.setClass(splash, Splash.class);
    
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcutname");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(splash, R.drawable.app_icon));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            splash.sendBroadcast(addIntent);
        }
        setContentView(R.layout.splash);
    

    Above is the code that I have used in my MAIN activity i.e. Splash. To prevent the creation of multiple shortcut icons I have applied a check before creating the shortcut that is to check if the app is being launched for the first time or not.

    MyUtility used above is a class where I have used SharedPreferences methods to save if the app is launched for the first time. Below is the MyUtility class:

    public class MyUtility 
    {
        MyUtility utility;
        public static String IS_FIRST_TIME="isFirstTime"; 
    
    

    //*****Save first time*****//

    public static void setIsFirstTime(Context con, String isFirstTime) { SharedPreferences isLoggedInShared = con.getSharedPreferences(IS_FIRST_TIME, 0); Editor loginEditor=isLoggedInShared.edit(); loginEditor.putString("isFirstTime",isFirstTime); loginEditor.commit(); } public static String getIsFirstTime(Context con) { SharedPreferences isLoggedInShared=con.getSharedPreferences(IS_FIRST_TIME,0); return isLoggedInShared.getString("isFirstTime", "null"); } }

    Also, in Manifest file add following permission:

    android:name="android.permission.READ_PHONE_STATE"
    android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
    android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"

    Hope it helps you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: