0501-app-launcher-shortcut

Source: Internet
Author: User

Determine if Shutcut is installed

<!-- 需要权限 -->

<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>

这段代码在模拟器上运行没有问题,但是在htc s510e运行报异常Failed to find provider info for com.android.launcher2.settings

通过但因htc所有包发现htc中的launcher被定制了改名为com.htc.launcher,所以这一段代码的通用行很差一般不对shortCut进行判断都是直接添加

 public        Boolean Isaddshortcut () {Boolean isinstallshortcut = false;        Final Contentresolver cr = This.getcontentresolver ();        int versionlevel = Android.os.Build.VERSION.SDK_INT;               String authority = "com.android.launcher2.settings";        More than 2.2 of the system's file names are not the same if (Versionlevel >= 8) {authority = "com.android.launcher2.settings";        } else {authority = "com.android.launcher.settings";        } final Uri Content_uri = Uri.parse ("content://" + Authority + "/favorites?notify=true"); Cursor C = cr.query (Content_uri, new string[] {"title", "Iconresource"}, "Title=?", New St        Ring[] {getString (r.string.app_name)}, NULL);        if (c! = null && c.getcount () > 0) {isinstallshortcut = true;    } return isinstallshortcut; }

Create shortcut on desktop shortcuts as soon as you join

Action android:name= "Android.intent.action.CREATE_SHORTCUT"/>

  <activity android:name= ". Activity01 "                  android:label=" @string/app_name ">            <intent-filter>         <action android:name=" Android.intent.action.CREATE_SHORTCUT "/>             </intent-filter>        </activity>    </ Application>

When you click this shortcut in Shotcutlist, the activity will open to execute the following code


public class Activity01 extends activity{public void onCreate (Bundle savedinstancestate) {super.oncreate (SA        Vedinstancestate);        Intent Addshortcut;        LOG.E ("-----------------------", Getintent (). Getaction () + ":" +intent.action_create_shortcut);            if (Getintent (). Getaction (). Equals (Intent.action_create_shortcut)) {addshortcut = new Intent ();            Addshortcut.putextra (intent.extra_shortcut_name, "Send Mail");              parcelable icon = Intent.ShortcutIconResource.fromContext (This,r.drawable.mail_edit);            Addshortcut.putextra (Intent.extra_shortcut_icon_resource,icon);            Intent mailto = new Intent (intent.action_sendto, Uri.parse ("Mailto:[email protected]");              Addshortcut.putextra (intent.extra_shortcut_intent, mailto);          Setresult (Result_ok,addshortcut);      } finish (); }}



Add shortcut directly to the desktop: again this method adds success to the emulator but fails in HTC s510e verification, which should also be the result of HTC's custom-made
Check the Android source to learn that the icon's additions (that is, the icon list inside) and shortcut are added by Recevier to accept notifications, but he defines the action:

public static final String action_install_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";

I suspect that this value of HTC may have changed.

Permission declarations

uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"

Intent shortcut = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT");        Set Property Shortcut.putextra (Intent.extra_shortcut_name, "SDJKDSF"); Shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext (This.getapplicationcontext (), R.drawable.ic        _launcher);         Shortcut.putextra (Intent.extra_shortcut_icon,iconres);               Whether to allow duplicate creation of Shortcut.putextra ("duplicate", false);               Set icon for desktop shortcut parcelable icon = Intent.ShortcutIconResource.fromContext (This,r.drawable.ic_launcher);               Shortcut.putextra (Intent.extra_shortcut_icon_resource,icon);        Click the action of the shortcut Intent Intent = new Intent (intent.action_main);        Intent.setflags (intent.flag_activity_reset_task_if_needed);        Intent.addflags (intent.flag_activity_launched_from_history);        Intent.addcategory (Intent.category_launcher);               Intent.setclass (Shortcutactivity.this, Shortcutactivity.class); Set the startup program Shortcut.putExtra (Intent.extra_shortcut_intent, Intent); Broadcast notification desktop to create this.sendbroadcast (shortcut);

Remove code and permission declarations for shortcut

uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"

Intent shortcut = new Intent ("Com.android.launcher.action.UNINSTALL_SHORTCUT");                         The name of the shortcut          Shortcut.putextra (intent.extra_shortcut_name, getString (R.string.app_name));          String Appclass = this.getpackagename () + "." +this.getlocalclassname ();          ComponentName comp = new ComponentName (This.getpackagename (), appclass);          Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (Intent.action_main). SetComponent (comp));                         


If there is a way to add shortcut directly and the general line is good, I want to enlighten you.

Related Article

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.