The following source code from the Launcher2.3 example
1. The main activity of each app will bring <category android:name= "Android.intent.category.LAUNCHER"/> Indicates that the app was installed to launcher and clicked to open the activity
<activity android:name= "org.lean.MainActivity" android:label= "@string/app_name" > < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= " Android.intent.category.LAUNCHER "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter> </activity>
The timing diagram of the 2.launcher2 source code is as follows: (in the figure, we can see that creating the shortcut requires 2 things to prepare, one action, and the other is the intent returned after we retrieved it)
2.1. When you want to create shortcut manually on the desktop, you must add a <action/> tag to the Androidmanifest.xml file.
as follows. We create a shortcutactivity to handle the creation of shortcut
<activity android:name= "org.lean.ShortCutActivity" > <intent-filter > <action android: Name= "Android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity>
2.2 and handles the return intent of the displayed shortcut style in activity
/** * @author Lean @date: 2014-8-25 */public class Shortcutactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate), if (Getintent (). Getaction (). Equals ( Intent.action_create_shortcut) {Intent returnintent=new Intent (); Returnintent.putextra (intent.extra_shortcut_ Icon_resource,intent.shortcuticonresource.fromcontext (This,r.drawable.ic_launcher)); ReturnIntent.putExtra ( Intent.extra_shortcut_name, "A simple SHORTCUT"); Returnintent.putextra (Intent.extra_shortcut_intent,new Intent ( This,mainactivity.class)); Setresult (result_ok,returnintent); Finish ();}}}
3. The above shortcut can only be added manually, if you want to add shortcut dynamically, you must send the broadcast. Android Launcher2 Source provides the following
<!--Intent received used to install shortcuts from other applications-- <receiver android:name= " Com.android.launcher2.InstallShortcutReceiver " android:permission=" Com.android.launcher.permission.INSTALL _shortcut "> <intent-filter> <action android:name=" com.android.launcher.action.INSTALL_ SHORTCUT "/> </intent-filter> </receiver>
This also means that we send the broadcast must declare the permissions, as well as the designation <action/>, and then add in our application Androidmanifest.xml
<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>
Also in code calls (also, dynamic addition of shortcut must specify its style and operational intent)
Intent intent=new Intent (); Intent.setaction ("Com.android.launcher.action.INSTALL_SHORTCUT"); Intent.putextra ( Intent.extra_shortcut_icon_resource,r.drawable.ic_launcher); Intent.putextra (Intent.extra_shortcut_name, "a auto Sample "); Intent.putextra (Intent.extra_shortcut_intent,new Intent (mainactivity.this,mainactivity.class)); Sendbroadcast (Intent);
The shortcut of Android-launcher Development (1)