Android judge and create shortcut (4.03 test pass)

Source: Internet
Author: User

Android judge and create shortcut (4.03 test pass)


Organized the online creation of the code, for the decision to use the system API to get the current initiator to handle, so the system customization or the initiator is not the same also does not matter.



One plus permission and declaring target activity

    <!--Create a shortcut--    <uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/ >    <uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>


  <activity            android:name= "com.shortcut.TestActivity"            android:configchanges= "keyboardhidden| Orientation "            android:theme=" @android: Style/theme.translucent ">            <intent-filter>                <action Android:name= "Action.com.shortcut.test"/>            </intent-filter>        </activity>

Two create code


/** * Create Shortcut * * @param context * @param name Display name * @param url */public static void CreateShortcut (context context, STR ing name, String URL) {if (Hasshortcut (context, name)) {LOG.V ("CreateShortcut", name + "shortcut already exists"); Final Intent shortcutintent = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT"); Shortcutintent.putextra (" Duplicate ", false); final parcelable icon = Intent.ShortcutIconResource.fromContext (Context,resourceutil.getid ( Context, "drawable", "O2o_game_float_icon"));//This parameter is the activated activity actionfinal Intent targetintent = new Intent (" Action.com.shortcut.test ");//target Activitytargetintent.setclassname (Context.getpackagename ()," Com.shortcut.TestActivity "); Targetintent.putextra (" url ", url); Targetintent.putextra (" name ", name); Targetintent.setflags (Intent.flag_activity_new_task); Shortcutintent.putextra (Intent.extra_shortcut_intent, targetintent); Shortcutintent.putextra (Intent.extra_shortcut_name, NAME); Shortcutintent.putextra (Intent.EXTRA_ Shortcut_icon_resource, icon); Context.sendbroadcast (shortcutintent);}  /** * Determine if a shortcut already exists * * @param context * @param name * @return */private Static Boolean hasshortcut (context context, String Name) {LOG.V ("Launcherpackagename", Getlauncherpackagename (context)); String Launcherpackage = getlauncherpackagename (context); if (Textutils.isempty (launcherpackage)) {// The shortcut is already present by default when you do not query the initiator, and does not create return true;} LOG.V ("Launcherpackagename", launcherpackage); Boolean result = false;final String uristr = "content://" + launcherpack Age+ ". Settings/favorites?notify=true"; final Uri Content_uri = Uri.parse (URISTR); final Cursor C = Context.getcontentresolver (). query (Content_uri, NULL, "Title=?", new string[] {name}, null); if (c! = null && c.ge TCount () > 0) {result = true;} return result;} /** * Gets the name of the running table bread (Note: when there are multiple desktops and the default desktop is not specified, the method returns "", which is used to handle this condition) */private static String Getlauncherpackagename (context Context) {Final Intent Intent = new Intent (intent.action_main); intent.addcategory (intent.category_home); final ResolveInfo Res= Context.getpackagemanager (). Resolveactivity (Intent, 0), if (Res.activityinfo = = null) {//should not happen. A Home is always installed, isn ' t it?return ";} if (Res.activityInfo.packageName.equals ("Android")) {//There are multiple desktop programs present, and no default items are specified; return "";} else {return res.activityInfo.packageName;}}


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.