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
Post a Comment