1、查询快捷图标是否已被创建
private boolean isInstallShortcut() { // TODO Auto-generated method stub //获取当前使用sdk的版本 int sdkInt = android.os.Build.VERSION.SDK_INT; if(sdkInt >= 8) { AUTHORITY = "com.android.launcher2.settings"; } else { AUTHORITY = "com.android.launcher.settings"; } Uri uri = Uri.parse("content://" + AUTHORITY + "/favorites"); String[] projection = new String[]{"title"}; String selection = "title = ?"; String[] selectionArgs = new String[]{"new shortcut"}; Cursor query = getContentResolver().query(uri, projection, selection, selectionArgs, null); return query.moveToNext(); }
2、创建快捷图标
public void installShortcut() {
// 查询是否已被创建 if(isInstallShortcut()) { Toast.makeText(this, "该快捷方式已创建", 0).show(); } else { Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //设置快捷方式的名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "new shortcut"); //快键方式的图标 ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); Intent myIntent = new Intent(); //设置一个主入口 myIntent.setAction("android.intent.action.MAIN"); myIntent.addCategory("android.intent.category.LAUNCHER"); //跳转至指定的activity ComponentName component = new ComponentName(this, OtherActivity.class); myIntent.setComponent(component); //跳转意图 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); sendBroadcast(intent); } }
3、删除快捷图标
public void unInstallShortcut() { if(!isInstallShortcut()) { Toast.makeText(this, "该快捷方式已被卸载或不存在", 0).show(); } else { // System.out.println("-------------------------unInstall"); Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); //指定快捷方式的名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "new shortcut"); Intent myIntent = new Intent(); //设置一个主入口 myIntent.setAction("android.intent.action.MAIN"); myIntent.addCategory("android.intent.category.LAUNCHER"); //跳转至指定的activity ComponentName component = new ComponentName(this, MainActivity.class); myIntent.setComponent(component); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); sendBroadcast(intent); } }
4、添加使用权限
<!-- 创建Shortcut权限 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <!-- 删除Shortcut权限 --> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> <!-- 读取设置配置权限 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />