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;}}