Elastic cut (1) for Android-Launcher development)

Source: Internet
Author: User

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);


 

 

 

 

 

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.