Android app Add (create) and delete and determine if there is a desktop shortcut-android novice-eoe Mobile Developer Forum-Powered by discuz!
The Android Desktop program provides the ability to add and remove desktop shortcuts and determine if a shortcut exists.
Just pass in the shortcut title, icon, and click on the shortcut to execute the app intent. The code is as follows:
1. Add desktop shortcuts to Android
/**
* Add desktop shortcuts to the current app
*
* @param CX
* @param appName
* Shortcut Name
*/
public static void Addshortcut (Context cx) {
Intent shortcut = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutintent = Cx.getpackagemanager ()
. Getlaunchintentforpackage (Cx.getpackagename ());
Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent);
Get the current app name
String title = null;
try {
Final Packagemanager PM = Cx.getpackagemanager ();
title = Pm.getapplicationlabel (
Pm.getapplicationinfo (Cx.getpackagename (),
Packagemanager.get_meta_data). toString ();
} catch (Exception e) {
}
Shortcut name
Shortcut.putextra (Intent.extra_shortcut_name, title);
Duplicate creation is not allowed (not necessarily valid)
Shortcut.putextra ("duplicate", false);
Icon for shortcut
Parcelable Iconresource = Intent.ShortcutIconResource.fromContext (CX,
R.drawable.ic_launcher);
Shortcut.putextra (Intent.extra_shortcut_icon_resource, Iconresource);
Cx.sendbroadcast (shortcut);
}
2. Android Delete Desktop shortcut
/**
* Remove desktop shortcuts for the current app
*
* @param CX
*/
public static void Delshortcut (Context cx) {
Intent shortcut = new Intent (
"Com.android.launcher.action.UNINSTALL_SHORTCUT");
Get the current app name
String title = null;
try {
Final Packagemanager PM = Cx.getpackagemanager ();
title = Pm.getapplicationlabel (
Pm.getapplicationinfo (Cx.getpackagename (),
Packagemanager.get_meta_data). toString ();
} catch (Exception e) {
}
Shortcut name
Shortcut.putextra (Intent.extra_shortcut_name, title);
Intent shortcutintent = Cx.getpackagemanager ()
. Getlaunchintentforpackage (Cx.getpackagename ());
Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent);
Cx.sendbroadcast (shortcut);
}
3, Android to determine whether the app already exists desktop shortcut
/**
* Determine if a shortcut has been added to the desktop
*
* @param CX
* @param titlename
* Shortcut Name
* @return
*/
public static Boolean hasshortcut (Context cx) {
Boolean result = false;
Get the current app name
String title = null;
try {
Final Packagemanager PM = Cx.getpackagemanager ();
title = Pm.getapplicationlabel (
Pm.getapplicationinfo (Cx.getpackagename (),
Packagemanager.get_meta_data). toString ();
} catch (Exception e) {
}
Final String Uristr;
if (Android.os.Build.VERSION.SDK_INT < 8) {
Uristr = "Content://com.android.launcher.settings/favorites?notify=true";
} else {
Uristr = "Content://com.android.launcher2.settings/favorites?notify=true";
}
Final Uri Content_uri = Uri.parse (URISTR);
Final Cursor C = cx.getcontentresolver (). query (Content_uri, NULL,
"Title=?", new string[] {title}, NULL);
if (c! = null && c.getcount () > 0) {
result = true;
}
return result;
}
4. Related Permissions Configuration
<uses-permission android:name="Com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="Com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="Com.android.launcher.permission.READ_SETTINGS" />
Android apps Add (create) and delete and determine if desktop shortcuts exist