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"/>