Skip to content

Instantly share code, notes, and snippets.

@zeuxisoo
Created June 5, 2011 13:44
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save zeuxisoo/1008973 to your computer and use it in GitHub Desktop.
Save zeuxisoo/1008973 to your computer and use it in GitHub Desktop.
[Android] Create and Remove Desktop Shortcut

Create

private void addShortcut(){  
	Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
          
	// Shortcut name
	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
	shortcut.putExtra("duplicate", false);	// Just create once

	// Setup current activity shoud be shortcut object 
	ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());  
	shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  
  
	// Set shortcut icon
	ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);  
	shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  

	sendBroadcast(shortcut);  
}

Remove

private void delShortcut(){  
	Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  
              
	// Shortcut name
	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
              
	String appClass = this.getPackageName() + "." +this.getLocalClassName();  
	ComponentName comp = new ComponentName(this.getPackageName(), appClass);  
	shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  
              
	sendBroadcast(shortcut);  
}

Permission

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment