Test environment for adnroid more than 2.1.
The first step: Androidmanifest.xml permission configuration:
To add shortcut permissions:
Copy Code code as follows:
<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>
Verify that the shortcut has permissions:
Copy Code code as follows:
<uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>
Delete Shortcut permissions:
Copy Code code as follows:
<uses-permission android:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
Code:
Copy Code code as follows:
public class Shortcutsample {
/**
* Add shortcuts
* */
public void Creatshortcut (activity activity,string shortcutname,int ResourceID)
{
Intent Intent = new Intent ();
Intent.setclass (activity, Activity.getclass ());
/* The following two sentences are in order to uninstall the application at the same time delete the desktop shortcuts * *
Intent.setaction ("Android.intent.action.MAIN");
Intent.addcategory ("Android.intent.category.LAUNCHER");
Intent shortcutintent = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT");
Do not allow duplicate creation
Shortcutintent.putextra ("duplicate", false);
Need a realistic name
Shortcutintent.putextra (Intent.extra_shortcut_name, shortcutname);
Quick picture
parcelable icon = Intent.ShortcutIconResource.fromContext (Activity.getapplicationcontext (), ResourceID);
Shortcutintent.putextra (Intent.extra_shortcut_icon_resource, ICON);
Click on the quick picture to run the program main entrance
Shortcutintent.putextra (Intent.extra_shortcut_intent, Intent);
Send a broadcast. Ok
Activity.sendbroadcast (shortcutintent);
}
/**
* Delete Shortcuts
* */
public void Deleteshortcut (activity activity,string shortcutname)
{
Intent shortcut = new Intent ("Com.android.launcher.action.UNINSTALL_SHORTCUT");
The name of the shortcut
Shortcut.putextra (Intent.extra_shortcut_name,shortcutname);
On the Internet to see the basic is a few sentences, testing the time found and can not delete shortcuts.
String Appclass = activity.getpackagename () + "." + Activity.getlocalclassname ();
ComponentName comp = new ComponentName (Activity.getpackagename (), appclass);
Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (Intent.action_main). SetComponent (comp));
/** can be deleted successfully, estimated to be deleted and created in order to find the shortcut and successfully delete the **/
Intent Intent = new Intent ();
Intent.setclass (activity, Activity.getclass ());
Intent.setaction ("Android.intent.action.MAIN");
Intent.addcategory ("Android.intent.category.LAUNCHER");
Shortcut.putextra (intent.extra_shortcut_intent,intent);
Activity.sendbroadcast (shortcut);
}
/**
* Determine if there are shortcuts
* */
public boolean hasshortcut (activity activity,string shortcutname)
{
String url = "";
int systemversion = Integer.parseint (Android.os.Build.VERSION.SDK);
/* More than 8 of the time in the Com.android.launcher2.settings query (not tested) * *
if (Systemversion < 8) {
url = "Content://com.android.launcher.settings/favorites?notify=true";
}else{
url = "Content://com.android.launcher2.settings/favorites?notify=true";
}
Contentresolver resolver = Activity.getcontentresolver ();
Cursor Cursor = resolver.query (uri.parse (URL), null, "Title=?", new string[] {shortcutname}, NULL);
if (cursor!= null && cursor.movetofirst ()) {
Cursor.close ();
return true;
}
return false;
}
}
To invoke the test code:
Copy Code code as follows:
public class Mainactivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Shortcutsample sample =new shortcutsample ();
String shortcutname=getstring (r.string.app_name);
if (Sample.hasshortcut (this, shortcutname))
Sample.deleteshortcut (This,shortcutname);
Else
Sample.creatshortcut (This,shortcutname,r.drawable.icon);
}
}
Looking for a long time on the internet is the same code, delete that piece of the afternoon just to get it done, in fact very simple dongdong.
The first time the article, adnroid New. A lot of communication and guidance ah. Oh.