How to add shortcuts to the desktop -- (detailed description of anycut project in open-source apps-for-android)

Source: Internet
Author: User
Tags home screen

If you describe a project in a language that seems pale and powerless, I just let him see what he sees and what he sees is what he gets. As shown in:

1. The project diagram is as follows:

2. Add shrotcut as follows:

3. After the shortcut cut is added, home screen * (desktop) is as follows:

After adding a shortcut to the desktop, you can directly return to home (desktop) when you want to run this program, and click this shortcut to run this program.

The code is analyzed as follows:

(1) Permissions

If your application has a shortcut function, you must add the corresponding permissions to the manifest file, that is:

<Uses-Permission Android: Name = "com. Android. launcher. Permission. install_shortcut"/>

(2) action of the activity that creates the shortcut:

Add the activity of shortcut. The action must be Android. Intent. Action. create_shortcut, as shown below. You can also set it in the Code:

<Activity Android: Name = ". createshortcutactivity">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. create_shortcut"/>
<Category Android: Name = "android. Intent. Category. Default"/>
</Intent-filter>
</Activity>

(3) code for creating a shortcut:

Public static final string
Action_create_shortcut added in API Level 1

Activity action: Creates a shortcut.

Input: Nothing.

Output: an intent representing the specified cut. the intent must contain three extras: shortcut_intent (value: intent), shortcut_name (value: string), and shortcut_icon (value: Bitmap) or combine (value: Your cuticonresource ).

An intent represents a shortcut. This intent must contain three parameters: the intent type of the shortcut (the value is the intent type), the name of the shortcut (the value is a string type), and the shortcut icon (the type is a bitmap) or shortcut icon Resource (value: shortcut icon resource type ).


// Build the intent for the chosen Activity

// Set the intent of the shrorcut
Intent intent = new intent ();
Intent. setcomponent (New componentname (info. activityinfo. applicationinfo. packagename,
Info. activityinfo. Name ));
Intent result = new intent ();
Result. putextra (intent. extra_shortcut_intent, intent );

// Set the name of the shortcut
Result. putextra (intent. extra_shortcut_name, info. loadlabel (mpackagemanager ));

// Build the icon info for shorcut
Drawable = info. loadicon (mpackagemanager );
If (drawable instanceof bitmapdrawable ){
Bitmapdrawable BD = (bitmapdrawable) drawable;
Result. putextra (intent. extra_shortcut_icon, Bd. getbitmap ());
}

// Set the result
Setresult (result_ OK, result );

(4) Send a broadcast after the creation is successful:

// Boradcast an intent that tells the home screen to create a new shortcut
Result. setaction ("com. Android. launcher. Action. install_shortcut ");
Sendbroadcast (result );

The following http://code.google.com/p/apps-for-android/ for open-source apps-for-android

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.