Desktop shortcuts for Android Desktop component development (1)

Source: Internet
Author: User

Home is the first application to interact with users after the Android device is enabled. Home will always run on the background when other applications are running on the foreground.

On an Android device with a screen resolution of 320*480 pixels, the desktop consists of 3 Fast 320 * PX areas when the screen is portrait, each area is divided into 16 80*100 pixel cells. The horizontal screen consists of three 424 * 296px cells, each screen is divided into 16 cells, but each cell is 106 * 74px. (The android2.2 system has now reached five screens ).

See the figure for specific dimensions:

Three types of components can be added to the launcher desktop: shortcuts, real-time folders, and desktop components. The Desktop shortcuts and real-time folders occupy the size of one cell in the desktop area, the desktop widget occupies one or more cells according to the functional requirements.

There are two ways to create shortcuts: 1. Build an intent in the application, and then notify launcher to create a shortcut in the form of broadcast; 2. register an intentfilter that meets specific conditions for the application's components, and then add a shortcut to start the component on the launcher's desktop.

Let's look at the code for adding shortcuts (first ):

Mainactivity Code: (layout Code omitted)

Code:
  1. Package com. myshortcut;
  2. Import Android. App. activity;
  3. Import Android. content. intent;
  4. Import android.net. Uri;
  5. Import Android. OS. Bundle;
  6. Import Android. OS. parcelable;
  7. Import Android. View. view;
  8. Import Android. View. View. onclicklistener;
  9. Import Android. widget. Button;
  10. /*
  11. * Desktop components-> shortcuts-> Add shortcuts to applications
  12. */
  13. Public class hellosponcut extends activity implements onclicklistener
  14. {
  15. Private button bt_fire = NULL;
  16. Private button bt_police = NULL;
  17. Private intent directCall = NULL;
  18. Private final string action_add_shortcut = "com. Android. launcher. Action. install_shortcut ";
  19. /** Called when the activity is first created .*/
  20. @ Override
  21. Public void oncreate (bundle savedinstancestate)
  22. {
  23. Super. oncreate (savedinstancestate );
  24. Setcontentview (R. layout. Main );
  25. Bt_fire = (button) findviewbyid (R. Id. bt_fire );
  26. Bt_police = (button) findviewbyid (R. Id. bt_police );
  27. Bt_fire.setonclicklistener (this );
  28. Bt_effece.setonclicklistener (this );
  29. DirectCall = new intent (intent. action_call );
  30. }
  31. @ Override
  32. Public void onclick (view V)
  33. {
  34. // Todo auto-generated method stub
  35. Intent addshortcut = new intent (action_add_shortcut );
  36. String nametodial = NULL;
  37. Parcelable icon = NULL;
  38. Switch (V. GETID ())
  39. {
  40. Case R. Id. bt_fire:
  41. Nametodial = "119 ";
  42. Icon = intent. Reset cuticonresource. fromcontext (this,
  43. R. drawable. Fire );
  44. Break;
  45. Case R. Id. bt_police:
  46. Nametodial = "110 ";
  47. /*
  48. * When cuticonresource is intent
  49. * An internal class is used to construct shortcuts, while intent implements the parcelable interface, so intent objects and
  50. * All cuticonresource objects can use putextra (string, parcelable) to input information
  51. * Add intent information to broadcast.
  52. */
  53. Icon = intent. Reset cuticonresource. fromcontext (this,
  54. R. drawable. Police );
  55. Break;
  56. Default:
  57. Break;
  58. }
  59. Addshortcut. putextra (intent. extra_shortcut_name, nametodial );
  60. Addshortcut. putextra (intent. extra_shortcut_icon_resource, icon );
  61. DirectCall. setdata (URI. parse ("Tel:" + nametodial ));
  62. Addshortcut. putextra (intent. extra_shortcut_intent, directCall );
  63. This. sendbroadcast (addshortcut );
  64. }
  65. }

Running result:

1,

2. (toast is provided by the system)

3. (After successful addition)

4. (To delete the shortcut icon, please press the icon long and drag it to the "drawer" area below to change the "drawer" to the "bin" icon)

Reference books: go deep into googleandroid

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.