Android apps Add (create) and delete and determine if desktop shortcuts exist

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.