Ii. Desktop Components
1. shortcuts
The shortcut on the Android phone can be understood by the shortcut of the Program on the actual PC machine. In Android, to create a program shortcut, you only need to press the desktop or click the menu button -- add, and select "Shortcuts" or "shortcut", as shown in, you can add shortcuts to your programs.
[Note]. In the program, how can we control the programs developed by ourselves to support shortcuts? Add the corresponding <action> in <intent-filter>, for example:
<activity android:label="@string/app_name"android:name=".ShortcutsActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.CREATE_SHORTCUT"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
Of course, the above process is based on the above configuration, so that your activity can find the activity of our program when triggering the Add shortcut action, and the name of the program shortcut, you can set icons and events in the activity. The code for this instance activity is as follows:
Public class shortcutsactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // intent addshortcut of the shortcut to be added; // determine whether to add a shortcut if (getintent (). getaction (). equals (intent. action_create_shortcut cut) {addshortcut = new intent (); // sets the shortcut name addshortcut. putextra (intent. extra_shortcut_name, "Send mail"); // you can specify parcelable icon = intent. using cuticonresource. fromcontext (this, R. drawable. mail_edit); // Add the shortcut icon addshortcut cut. putextra (intent. extra_shortcut_icon_resource, icon); // construct the intent mailtointent = new intent (intent. action_sendto, Uri. parse ("mailto: jercy@163.com"); // Add a shortcut intent addshortcut. putextra (intent. extra_shortcut_intent, mailtointent); setresult (result_ OK, addshortcut);} else {// cancel setresult (result_canceled);} finish ();}}
In the above Code, it is important to note: parcelable icon = intent. shortcuticonresource.Fromcontext(This, R. drawable.Mail_edit);
Android provides a dedicated way to build shortcuts.
The main. xml file has not changed. For the string. xml file:
<String name = "hello"> Create a desktop shortcut! </String> <string name = "app_name"> Desktop shortcuts </string>
The instance effect is as follows:
Picture on the left: click "add shortcut" and select related programs;
Figure right: After clicking our application, the desktop displays the shortcut, that is, setting the name and icon for the above program.
2. livefolders)
Real-time folder. This function has been implemented in the previous notepad instance, and the content displayed is updated in real time. You can view it without opening the relevant program separately, for example: view emails and contacts.
[Note]: Real-Time folders do not store information. They all view the data information pointed to by their contentprovider in ing mode, and can customize the display format. During this time, the source data changes, you can also update the displayed content in real time.
Similar to the preceding shortcuts, you also need to configure related actions.
<activity android:label="@string/app_name"android:name=".LiveFoldersActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <action android:name= "android.intent.action.CREATE_LIVE_FOLDER" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
Corresponds to Activiy:
Public class livefoldersactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // determines whether to wear a Real-Time folder if (getintent (). getaction (). equals (livefolders. action_create_live_folder) {intent = new intent (); // sets the data address intent. setdata (URI. parse ("content: // contacts/live_folders/People"); // set the time after the click. Click a contact and call intent. putextra (livefolders. extra_live_folder_base_intent, new intent (intent. action_call, contactscontract. contacts. content_uri); // sets the real-time file name intent. putextra (livefolders. extra_live_folder_name, "your own phone book"); // sets the intent icon of the real-time folder. putextra (livefolders. extra_live_folder_icon, intent. using cuticonresource. fromcontext (this, R. drawable. contacts); // sets the display mode to list mode intent. putextra (livefolders. extra_live_folder_display_mode, livefolders. display_mode_list); setresult (result_ OK, intent);} else {setresult (result_canceled) ;}finish ();}}
Instance effect:
Figure left: click Add folder and select related programs;
Graph: After clicking our application, the real-time folder icon is displayed on the desktop.
Figure right: click the "own phone book" icon to display the corresponding information in the address book.
There is also a desktop component widget, which will be supplemented later. To sum up the two small functions above, it is relatively simple and practical.