Android开发之创建桌面快捷方式

Android创建桌面快捷方式就是在桌面这个应用上创建一个快捷方式,桌面应用:launcher2

通过查看launcher2的清单文件:

1         <!-- Intent received used to install shortcuts from other applications -->
2         <receiver
3             android:name="com.android.launcher2.InstallShortcutReceiver"
4             android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
5             <intent-filter>
6                 <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
7             </intent-filter>
8         </receiver>

可以看出来,创建桌面快捷方式,是通过Broadcast。

那么想要创建一个快捷方式的话,就发送这个广播就可以了。

同时注意,清单文件中写的很清楚,需要权限:com.android.launcher.permission.INSTALL_SHORTCUT,所以需要在清单文件中添加权限。

同时需要在添加快捷方式的名称,图标和该快捷方式干什么事情(一个含有action等信息的intent)

携带的信息:使用intent.pueExtra()

名称:Intent.EXTRA_SHORTCUT_NAME

图标:Intent.EXTRA-SHORTCUT_ICON

意图:Intent.EXTRA_SHORTCUT_INTENT

创建桌面快捷方式的代码:

 1         Intent intent = new Intent();
 2         intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
 3         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "桌面快捷方式");
 4         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
 5         Intent shortIntent = new Intent();
 6         shortIntent.setAction("android.intent.action.MAIN");
 7         shortIntent.addCategory("android.intent.category.LAUNCHER");
 8         shortIntent.setClassName(getPackageName(), "com.xxx.mobile.activity.WelcomeActivity");
 9         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortIntent);
10         sendBroadcast(intent);

通过以上代码,就可以创建一个桌面快捷方式了。

原文地址:https://www.cnblogs.com/liyiran/p/5231102.html