Elastic cut (1) for Android-Launcher development)
The following source code is from Launcher2.3.
1. By default, the main Activity of each application comes Indicates that when the application is installed to Launcher, click to open the Activity
2. the time sequence diagram of Launcher2 source code is as follows: (in the figure, we can see that two items need to be prepared to create the shortcut, one action, and the other is the intent returned after the query)
2. 1. to manually create a shortcut on the desktop, you must add a tag to the AndroidManifest. xml file.
As shown in the following figure. We create a ShortCutActivity to process the creation of the shortcut.
2.2 and process the returned Intent of the displayed shortCut style in the 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 preceding export cut can only be manually added. If you want to dynamically Add the export cut, broadcast must be sent. Android Launcher2 Source Code provides the following
This also indicates that the permission must be declared and specified for sending broadcasts, So we add the permission in our application AndroidManifest. xml.
At the same time, you can call the code (Meanwhile, you must also specify the style and operation intent to dynamically Add the shortcut)
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);