The test environment is Adnroid 2.1 or above.
Step 1: AndroidManifest. xml permission Configuration:
Add shortcut permissions:
Copy codeThe Code is as follows: <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
Verify that the shortcut has the following permissions:Copy codeThe Code is as follows: <uses-permission android: name = "com. android. launcher. permission. READ_SETTINGS"/>
Delete shortcuts:Copy codeThe Code is as follows: <uses-permission android: name = "com. android. launcher. permission. UNINSTALL_SHORTCUT"/>
Code:Copy codeThe Code is as follows: public class ShortCutSample {
/**
* Add shortcuts
**/
Public void createqualcut (Activity activity, String shortcutName, int resourceId)
{
Intent intent = new Intent ();
Intent. setClass (activity, activity. getClass ());
/* The following two statements are used to delete the desktop shortcut when you detach an application */
Intent. setAction ("android. intent. action. MAIN ");
Intent. addCategory ("android. intent. category. LAUNCHER ");
Intent shortcutintent = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT ");
// Repeated creation is not allowed
Shortcutintent. putExtra ("duplicate", false );
// The actual name is required
Shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_NAME, shortcutName );
// Quick image
Parcelable icon = Intent. javascuticonresource. fromContext (activity. getApplicationContext (), resourceId );
Shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon );
// Click the shortcut image to run the main program entry
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 ");
// Shortcut name
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, shortcutName );
// You can see a few words on the Internet. You cannot delete shortcuts during the test.
// 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 ));
/** Changed to the following method to be successfully deleted. It is estimated that the shortcut can be found and deleted successfully only after deletion and creation **/
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 whether a shortcut exists
**/
Public boolean hasShortcut (Activity activity, String shortcutName)
{
String url = "";
Int systemversion = Integer. parseInt (android. OS. Build. VERSION. SDK );
/* Query (not tested) in com. android. launcher2.settings when the value is greater than 8 )*/
If (systemversion <8 ){
Url = "content: // com. android. launcher. settings/favorites? Required y = true ";
} Else {
Url = "content: // com. android. launcher2.settings/favorites? Required y = 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;
}
}
Call the test code:Copy codeThe Code is as follows: public class mainActivity extends Activity {
/** Called when the activity is first created .*/
@ 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. createconcut (this, shortcutName, R. drawable. icon );
}
}
I searched for the same code on the Internet for a long time. It took me one afternoon to delete the code. It was actually very simple.
The first time I published an article, new Adnroid. Provide more communication and guidance. Haha.