Skip to main content

Automatically Create Application Shortcut Icon On Home Screen

1] Create Code In JAVA File:

public class MainActivity extends Activity {
       Context mContext=MainActivity.this;
       SharedPreferences appPreferences;
       boolean isAppInstalled = false;
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              /**
               * check if application is running first time, only then create shorcut
               */
              appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
              isAppInstalled = appPreferences.getBoolean("isAppInstalled",false);
              if(isAppInstalled==false){
              /**
               * create short code
               */
              Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
              shortcutIntent.setAction(Intent.ACTION_MAIN);
              Intent intent = new Intent();
              intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
              intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo");
              intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo));
              intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
              getApplicationContext().sendBroadcast(intent);
              /**
               * Make preference true
               */
              SharedPreferences.Editor editor = appPreferences.edit();
              editor.putBoolean("isAppInstalled", true);
              editor.commit();
       }
}
}
2] Write A Code In AndroidManifest.xml

   <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 Output:
         Enjoy The App...........Code................For Your Application...

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"