Launcher is the first program launched after the system starts, is the entrance to other applications, that is, our mobile phone program Desktop program;
I. Definition and composition of launcher:
<1> by viewing the official launcher source code can know in fact launcher is also an activity, but its intent-fliter a bit special;
<Activity Android:name= "Launcher"Android:launchmode= "Singletask"Android:cleartaskonlaunch= "true"android:statenotneeded= "true"Android:theme= "@android: Style/theme.wallpaper.notitlebar"android:screenorientation= "Nosensor"Android:windowsoftinputmode= "Stateunspecified|adjustpan" > <intent-filter> <action android:name= "android.intent. Action. MAIN "/> <category android:name=" Android.intent.category.HOME "/> <category Andro Id:name= "Android.intent.category.DEFAULT"/> <category android:name= "Android.intent.category.MONKEY" /> </intent-filter> </activity>
In the intent-filter of launcher, Action is Intent.action.MAIN, indicating that the activity is the main entry of the program, but its category is Category.home, and the general app is not the same, Category.home identifies the activity is a launcher , in fact, the corresponding press the home button to jump to the activity, that is, our desktop;
Let's take a look at the configuration of the main entry activity for a common app program:
<action android:name= "Android.intent.action.MAIN"/> indicates that the class is the main entrance of the program;
Category. Launcher indicates that the activity is visible on the launcher, so this activity is added to the launcher;
<2>launcher Composition:
Homescreen (Workspace+hotseats), Shortcut (shortcut), Livefolder (folder), Appwidget (widgets), wallpaper (wallpaper);
Allapplist:
Let's explore these four elements separately.
1, Shortcut
In the launcher configuration file There is a broadcast receiver for listening to add shortcuts
<receiver android:name= ". Installshortcutreceiver " android:permission=" Com.android.launcher.permission.INSTALL_SHORTCUT " > <intent-filter> <action android:name= "Com.android.launcher.action.INSTALL_SHORTCUT"/> </intent-filter> </receiver>
View Installshortcutreceiver's source code
Public classInstallshortcutreceiverextendsBroadcastreceiver {Private Static FinalString action_install_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT"; Private Final int[] Mcoordinates =New int[2]; Public voidOnReceive (context context, Intent data) {if(!action_install_shortcut.equals (Data.getaction ())) { return; } intScreen =Launcher.getscreen (); if(!installshortcut (context, data, screen)) { //the target screen was full and let's try the other screens for(inti = 0; i < Launcher.screen_count; i++) { if(I! = Screen && installshortcut (context, data, i)) Break; } } }}
As you can see from the core code above, after Broadcastreceiver receives the Install_shortcut broadcast, it tries to add a shortcut to the current screen, and if the current screen is full, it tries to add the shortcut to the other screen; When we want to add a shortcut to the screen, it is actually very simple, send a broadcast, and set its action to Install_shortcut, but it is important to note that if you just set the action is not enough, because simply doing this will add a shortcut repeatedly, If you want to not repeat the addition, you have to do some settings, details refer to here;
2, Livefolder
In the Launcher.java file, locate the entry that added the Livefolder
//Insert Extra item to handle inserting folderBundle bundle =NewBundle (); ArrayList<String> Shortcutnames =NewArraylist<string>(); Shortcutnames.add (res.getstring (R.string.group_folder)); Bundle.putstringarraylist (Intent.extra_shortcut_name, shortcutnames); ArrayList<ShortcutIconResource> shortcuticons =NewArraylist<shortcuticonresource>(); Shortcuticons.add (Shortcuticonresource.fromcontext (Launcher. This, R.drawable.ic_launcher_folder)); Bundle.putparcelablearraylist (Intent.extra_shortcut_icon_resource, shortcuticons); Intent pickintent=NewIntent (intent.action_pick_activity); Pickintent.putextra (Intent.extra_intent,NewIntent (Livefolders.action_create_live_folder)); Pickintent.putextra (Intent.extra_title, GetText (R.string.title_select_live_folder)); Pickintent.putextras (bundle); Startactivityforresult (Pickintent, request_pick_live_folder);
When we long press the desktop, choose to add a folder, then the above code will be executed, here will first create an empty folder;
void Addlivefolder (Intent Intent) {//result_pick_live_folder // String folderName = Getresources (). GE Tstring (R.string.group_folder); String shortcutname = Intent.getstringextra (intent.extra_shortcut_name); if (folderName! = null && Foldername.equals (shortcutname)) {AddFolder (!) mdesktoplocked); else {Startactivityforresult (inten T, Request_create_live_folder); } }
Complete the Add
Private voidCompleteaddlivefolder (Intent data, Celllayout.cellinfo Cellinfo,BooleanInsertatfirst) { //Request_create_live_folder Cellinfo.screen=Mworkspace.getcurrentscreen (); if(!findsingleslot (Cellinfo))return; FinalLivefolderinfo info = Addlivefolder ( This, data, Cellinfo,false); if(!mrestoring) {Smodel.adddesktopitem (info); FinalView view = Livefoldericon.fromxml (R.layout.live_folder_icon, This, (ViewGroup) Mworkspace.getchildat (Mworkspace.getcurrentscreen ()), info); Mworkspace.addincurrentscreen (view, CELLINFO.CELLX, cellinfo.celly,1, 1, Insertatfirst); } Else if(smodel.isdesktoploaded ()) {Smodel.adddesktopitem (info); } }
3, Appwidget:appwidgetprovider used to display the plugin on the home page
Implementation steps:
1, for Appwidget to provide a meta-layout file Appwigdetprovider_provider.xml, used to display the Widget interface.
2. Create a class that inherits from Appwidgetprovider and overwrite the relevant method inside and registers to the configuration file.
3. Create a referenced layout file for Widgetprovider.
The code is as follows:
1. Create the appwigdetprovider_provider. xml file under the Res/xml/folder
<?XML version= "1.0" encoding= "Utf-8"?><Appwidget-providerxmlns:android= "Http://schemas.android.com/apk/res/android"Android:initiallayout= "@layout/main"<!--android:initiallayout Set the referenced layout file-->android:minheight= "50dip" android:minwidth= "50dip" android:updateperiodmillis= "> "<!--set the update time in milliseconds -</Appwidget-provider>
2, modify mainactivity inherit from appwidgetprovider and overwrite some of the methods inside, in fact Appwidgetprovider is a broadcastreceiver;
Public classMainactivityextendsAppwidgetprovider {@Override Public voidOnUpdate (context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) { Super. OnUpdate (Context, Appwidgetmanager, appwidgetids); Timer Timer=NewTimer (); Timer.scheduleatfixedrate (NewLytimetask (context, Appwidgetmanager), 1, 50000); } Private classLytimetaskextendsTimerTask {remoteviews remoteviews; Appwidgetmanager Appwidgetmanager; ComponentName widgets; @Override Public voidRun () {Date Date=NewDate (); Calendar Calendar=NewGregorianCalendar (2013, 07, 24); LongDays = (Calendar.gettimeinmillis ()-date.gettime ())/1000/86400; Remoteviews.settextviewtext (R.id.worldcup,"Still remaining" + days + "Day"); Appwidgetmanager.updateappwidget (widgets, remoteviews); } PublicLytimetask (Context context, Appwidgetmanager Appwidgetmanger) {Super(); This. Appwidgetmanager =Appwidgetmanger; Remoteviews=Newremoteviews (Context.getpackagename (), r.layout.activity_main); Widgets=NewComponentName (context, mainactivity.class); } };}
3 . Create a display layout file for the widget: Main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match _parent " android:layout_height=" Match_parent " android:background=" @drawable/worldcup " Android:orientation= "vertical" > <TextView android:id= "@+id/babybirthday" android:layout_w Idth= "Fill_parent" android:layout_height= "Wrap_content" android:text= "@string/message" android:textsize= "12px" android:textcolor= "#ff0000"/></linearlayout>
4, modify the program automatically generated manifest file. In Androidmanifest.xml, the subclass of the Appwidgetprovider declared above is a receiver, and:
(1), the Intent-filter action of the receiver must contain "Android.appwidget.action.APPWIDGET_UPDATE";
(2), the receiver's meta-data is "Android.appwidget.provider", and an XML file to describe the layout properties.
<ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <receiverAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.appwidget.action.APPWIDGET_UPDATE" /><!--Broadcast receive filter - </Intent-filter> <Meta-dataAndroid:name= "Android.appwidget.provider"Android:resource= "@xml/appwigdetprovider_provider" /><!--Appwidgetprovider references to provider files - </receiver></Application>
Run the program: Enter the Widgets page to add widgets to the home page
In the Appwidgetprovider class, there are other related methods
Public classWidgetproviderextendsAppwidgetprovider {//every time a broadcast message is received, it is called once, with frequent Public voidOnReceive (Context context, Intent Intent) {Super. OnReceive (context, intent); } //This method is called once per update, using frequent Public voidOnUpdate (context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) { Super. OnUpdate (Context, Appwidgetmanager, appwidgetids); } //Call every time you delete one Public voidOndeleted (Context context,int[] appwidgetids) { Super. ondeleted (context, appwidgetids); } //This method is called when the widget is first added to the desktop and can be added multiple times but only for the first time Public voidonenabled (Context context) {Super. onenabled (context); } //when the last widget is deleted is called the method, note that the last one Public voidondisabled (Context context) {Super. ondisabled (context); }}
Define the app widgets layout file
Defining Appwidgetproviderinfo Resources
Creating the Appwidgetprovider Class
Create an app widgets configuration activity
App Widgets is essentially based on the remoteviews implementation, in order to display the app Widgets, you need to first create a layout file;
The Appwidgetproviderinfo resource defines the metadata for the app widgets, such as the update interval, layout file, and configuration activity.
Android Launcher Research Learning