Android 为应用程序中的任意Activity创建桌面快捷方式
在网络上可以随便的找到,shortcut桌面快捷方式的使用程序,包括添加、删除、检查是否存在。但是他们都是新建某个应用程序的快捷方式,下面我要讲解的是新建自身应用中的某个子Activity的桌面快捷方式。
1、工程结构图如下
MainActivity 是应用程序的启动入口,TestActivity1是要为你新建的多个桌面快捷方式的activity,以实现点击不同的快捷方式时都能够跳转到同一个界面,通过跳转时快捷方式的Intent中携带的内容,来判断这个TestActivity1里面到底到执行哪些操作。
2、手动创建桌面快捷方式代码
/** * 创建快捷方式 * title: 快捷方式的标题 * intentContent:点击快捷方式时,向目标Activity发送的意图中附带的内容 * cls: 快捷方式目标类 * action:设置快捷方式目标的action,这个东西主要是在删除该快捷方式的时候用到,用来指定删除的快捷方式,一定要相同,如果不同无法删除 * */ public void createShortCut(String title, String intentContent, Class cls, String action){ // 创建添加快捷方式的intent Intent addIntent = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 允许重复创建 addIntent.putExtra("duplicate", true);//这个设置没有什么用,就算这只成了允许重复创建,依然不能重复创建 // 加载快捷方式图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_attach_file_icon_excel_large); // 创建点击快捷方式后启动的程序,这里启动TestActivity1 Intent purposeIntent = new Intent(this, cls); purposeIntent.putExtra("intentContent", intentContent); //设置action,这个action主要是在删除该快捷方式的时候用的上,删除快捷方式时意图action一定要和这个一样否则无法删除 purposeIntent.setAction(action); // 设置快捷方式的标题 addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); // 设置快捷方式的图标 addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 设置快捷方式点击时对应的Intent addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, purposeIntent); // 发送广播,添加快捷方式 sendBroadcast(addIntent); } |
3、手动删除指定快捷方式代码
/** * 删除程序的快捷方式 * packageName:快捷方式映射的对象的包 例如:"com.example.createshortcut", * appClass:快捷方式映射的对象的class 例如:"com.example.createshortcut.TestActivity1" * shortcutName:要删除的快捷方式名称 * action:指定要删除的快捷方式的action,这个值和添加快捷方式时的值一定要对应,否则无法删除 */ public void delShortcut(Context context, String packageName, String appClass, String shortcutName, String action) { Intent shortcut = new Intent( "com.android.launcher.action.UNINSTALL_SHORTCUT"); // 快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName); ComponentName comp = new ComponentName(packageName, appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action ).setComponent(comp)); context.sendBroadcast(shortcut); } |
4、配置Menifest.xml文件
在menifest下添加如下权限
<!-- 添加快捷方式 权限: --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <!-- 验证快捷方式是否存在 权限 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <!-- 删除快捷方式 权限 --> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> |
在application下添加TestActivity1的注册信息
特别注意:这个activity下面,添加了
<action android:name="android.intent.action.CREATE_SHORTCUT"></action>
这个一定要有,网络上很多创建快捷方式的程序都只能跳转到主应用,就是应为没有为想要跳转的应用添加这条action。 所以才会出现,如果指定跳转到其他activity后,点击快捷方式之后提示:未安装该应用。
<activity android:name="com.example.createshortcut.TestActivity1" > <!-- 如果不添加这个action 创建快捷方式是没有问题,但是快捷方式点击之后提示:未安装该应用 --> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> |
5、 顺便说一下判断某个快捷方式是否存在
在网络上搜索到示例,这个方法在本程序中没有作用。具体原因暂时未知
当然如果实在知道这个快捷方式是否已经创建了,也可采用在每次创建快捷方式时都将是否创建的状态保存到自己的数据库中,并在程序中写好这个数据库的内容提供者,在判断快捷方式时直接到自己指定的url上去获取内容提供。也就是修改
url = "content://com.android.launcher.settings/favorites?notify=true";
为自己的内容提供url,这样就保证万无一失了。
/** * 判断是否存在快捷方式 * */ public boolean hasShortcut(Activity activity, String shortcutName) { String url = ""; int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK); Log.d("testActivity", "systemversion "+systemversion); /* 大于8的时候在com.android.launcher2.settings 里查询(未测试) */ if (systemversion < 8) { url = "content://com.android.launcher.settings/favorites?notify=true"; } else { url = "content://com.android.launcher2.settings/favorites?notify=true"; } ContentResolver resolver = activity.getContentResolver(); Cursor cursor = resolver.query(Uri.parse(url), null, "title=?", new String[] { shortcutName }, null); Log.d("testActivity", "cursor "+cursor); if (cursor != null && cursor.moveToFirst()) { cursor.close(); return true; } return false; } |
程序实例下载地址:http://download.csdn.net/detail/xzwjraohuamin1/7521287