Create a desktop shortcut icon

Source: Internet
Author: User
The source code of the desktop program is analyzed to find the broadcast receiver that receives and creates the desktop shortcut icon. The method of creating a shortcut mutation is to send the broadcast. The following describes how to create a desktop shortcut icon. 1. In the first activity of the application, add the method for creating the shortcut icon, installshortcut ();
// Create a desktop shortcut icon
private void intallShotCut() {
// Define a shortcut icon for creating a broadcast notification Desktop
    Intent intent = new Intent();
Intent. setaction ("com. Android. launcher. Action. install_shortcut"); // specify the intent
// Set the action (shortcut name and Icon)
Intent. putextra (intent. extra_shortcut_name, "small guard ");
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// Set the Enable intent
    Intent homeIntent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
Sendbroadcast (intent); // send a broadcast that creates a shortcut icon
}

Remember to add permissions

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
2. Find and No bound to open our programWhy?

Because desktop applications are different from our applications, we can only use implicit intent instead of explicit intent.

The configuration of the desktop activity configuration file is as follows:

<activity android:name="com.itheima.mobilesafe.activities.HomeActivity" >
    <intent-filter>
        <action android:name="com.itheima.mobilesafe.home" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Installshortcut (); changed:

// Create a desktop shortcut icon
private void intallShotCut() {
// Define a shortcut icon for creating a broadcast notification Desktop
    Intent intent = new Intent();
Intent. setaction ("com. Android. launcher. Action. install_shortcut"); // specify the intent
// Set the action (shortcut name and Icon)
Intent. putextra (intent. extra_shortcut_name, "small guard ");
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// Set the implicit enable intent
    Intent homeIntent = new Intent();
    homeIntent.setAction("com.itheima.mobilesafe.home");
    homeIntent.addCategory("android.intent.category.DEFAULT");
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
Sendbroadcast (intent); // send a broadcast that creates a shortcut icon
}
3. But there is a small bug. Every time you enter the program, a shortcut icon will be created on the desktop. Check whether the final code list of the first running program is as follows:
// Create a desktop shortcut icon
private void intallShotCut() {
    boolean installedshortcut = sp.getBoolean("installedshortcut", false);
    if(installedshortcut) {
return;
    }
// Define a shortcut icon for creating a broadcast notification Desktop
Intent intent = new Intent();
Intent. setaction ("com. Android. launcher. Action. install_shortcut"); // specify the intent
// Set the action (shortcut name and Icon)
Intent. putextra (intent. extra_shortcut_name, "small guard ");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// Set the implicit enable intent
Intent homeIntent = new Intent();
homeIntent.setAction("com.itheima.mobilesafe.home");
homeIntent.addCategory("android.intent.category.DEFAULT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
Sendbroadcast (intent); // send a broadcast that creates a shortcut icon
// Save the installation shortcut icon Information
Editor editor = sp.edit();
editor.putBoolean("installedshortcut", true);
editor.commit();
}

The configuration of the desktop activity configuration file is as follows:

<activity android:name="com.itheima.mobilesafe.activities.HomeActivity" >
    <intent-filter>
        <action android:name="com.itheima.mobilesafe.home" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Remember to add permissions

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

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.