创建和删除快捷方式主要是Launcher完成的,我们只是把快捷方式的名称、图标、关联的activity信息传递给Launcher而已。
我们构造好了Intent信息后,将其以广播的方式发送出去,Launcher会帮忙完成创建、删除,并且会弹出一个Toast。
1.下面是系统中Launcher的Manifest文件。从中可以看到它定义了创建和删除快捷方式的permission,因此我们必须在自己的应用中声明权限才行。
com.android.launcher.permission.INSTALL_SHORTCUT 创建权限
com.android.launcher.permission.UNINSTALL_SHORTCUT 删除权限
2.创建和删除快捷方式的广播为InstallShortcutReceiver 和 UninstallShortcutReceiver。
3.Launcher为我们提供了ContentProvider LauncherProvider,我们可以通过查看它来获取已经存在的快捷方式的信息 。
这个也是要有permission(com.android.launcher.permission.READ_SETTINGS) 。
Launcher的manifest文件
xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.launcher">
<original-package android:name="com.android.launcher2" /> <permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="normal" android:label="@string/permlab_install_shortcut" android:description="@string/permdesc_install_shortcut" /> <permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="normal" android:label="@string/permlab_uninstall_shortcut" android:description="@string/permdesc_uninstall_shortcut"/> <permission android:name="com.android.launcher.permission.READ_SETTINGS" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="normal" android:label="@string/permlab_read_settings" android:description="@string/permdesc_read_settings"/> <permission android:name="com.android.launcher.permission.WRITE_SETTINGS" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" android:protectionLevel="normal" android:label="@string/permlab_write_settings" android:description="@string/permdesc_write_settings"/> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.SET_WALLPAPER" /> <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.BIND_APPWIDGET" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" /> <application android:name="com.android.launcher2.LauncherApplication" android:label="@string/application_name" android:icon="@drawable/ic_launcher_home" android:hardwareAccelerated="@bool/config_hardwareAccelerated" android:largeHeap="@bool/config_largeHeap"> <activity android:name="com.android.launcher2.Launcher" android:launchMode="singleTask" android:clearTaskOnLaunch="true" android:stateNotNeeded="true" android:theme="@style/Theme" android:windowSoftInputMode="adjustPan" android:screenOrientation="nosensor"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> </intent-filter> </activity> <activity android:name="com.android.launcher2.WallpaperChooser" style="@style/Theme.WallpaperPicker" android:label="@string/pick_wallpaper" android:icon="@drawable/ic_launcher_wallpaper" android:finishOnCloseSystemDialogs="true" android:process=":wallpaper_chooser"> <intent-filter> <action android:name="android.intent.action.SET_WALLPAPER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.wallpaper.preview" android:resource="@xml/wallpaper_picker_preview" /> </activity> <activity android:name="com.android.launcher2.RocketLauncher" android:label="@string/dream_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DREAM" /> </intent-filter> </activity> <!-- Intent received used to install shortcuts from other applications --> <receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> <intent-filter> <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" /> </intent-filter> </receiver> <!-- Intent received used to uninstall shortcuts from other applications --> <receiver android:name="com.android.launcher2.UninstallShortcutReceiver" android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT"> <intent-filter> <action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" /> </intent-filter> </receiver> <!-- The settings provider contains Home's data, like the workspace favorites --> <provider android:name="com.android.launcher2.LauncherProvider" android:authorities="com.android.launcher2.settings" android:writePermission="com.android.launcher.permission.WRITE_SETTINGS" android:readPermission="com.android.launcher.permission.READ_SETTINGS" /> </application></manifest>
4.Launcher的表创建如下
);
下面是一个实例程序。
源码:
101 }
manifest文件
26 </manifest>