ShortCutUtils 快捷方式工具类

package org.droid.util.sys; import android.app.Activity; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.Intent.ShortcutIconResource; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; /** * @author coffee */ public class ShortCutUtils { static final String ACTION_INSTALL = "com.android.launcher.action.INSTALL_SHORTCUT"; static final String ACTION_UNINSTALL = "com.android.launcher.action.UNINSTALL_SHORTCUT"; /** * 创建快捷方式 * 需要权限 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> */ public static void createShortcut(Activity context, Class<?> startupActivityClass, int appName, int icon) { Intent shortcut = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(appName)); shortcut.putExtra("duplicate", false); // 不允许重复创建 // Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.setClass(context.getApplicationContext(), startupActivityClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 快捷方式的图标 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext( context, icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); context.sendBroadcast(shortcut); } /** * 删除快捷方式 * 需要权限 <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> */ public static void delShortcut(Activity context) { /* * //该代码不管用,决定采用从数据库中删除的办法 Intent shortcut = new * Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); * shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, * context.getString(R.string.app_name)); ComponentName * localComponentName = new ComponentName(context.getPackageName(), * "."+context.getLocalClassName()); Intent intent = new * Intent(Intent.ACTION_MAIN).setComponent(localComponentName); * shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); * context.sendBroadcast(shortcut); return; */ String dbPath = "/data/data/com.android.launcher/databases/launcher.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); db.delete("favorites", "iconPackage=?", new String[] { context .getApplication().getPackageName() }); db.close(); } /** * 检测快捷方式是否存在 * 需要权限<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> * @param context */ public static boolean isExistShortcut(Activity context) { boolean isInstallShortcut = false; final ContentResolver cr = context.getContentResolver(); String AUTHORITIES = ""; if(android.os.Build.VERSION.SDK_INT < 8){ AUTHORITIES = "com.android.launcher2.settings"; }else{ AUTHORITIES = "com.android.launcher.settings"; } final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "iconPackage" }, "iconPackage=?", new String[] { context.getApplication() .getPackageName() }, null); if (c != null) { if (c.getCount() > 0) { isInstallShortcut = true; } c.close(); } return isInstallShortcut; } public static void delShortcutFromDesktop(Context paramContext, String paramString1, String paramString2, String paramString3) { Intent localIntent1 = new Intent( "com.android.launcher.action.UNINSTALL_SHORTCUT"); String str = paramString3; PackageManager localPackageManager = paramContext.getPackageManager(); int i = 8320; try { ApplicationInfo localApplicationInfo = localPackageManager .getApplicationInfo(paramString1, i); if (str == null) str = localPackageManager.getApplicationLabel( localApplicationInfo).toString(); localIntent1.putExtra("android.intent.extra.shortcut.NAME", str); ComponentName localComponentName = new ComponentName(paramString1, paramString2); Intent localIntent2 = new Intent("android.intent.action.MAIN") .setComponent(localComponentName); localIntent1.putExtra("android.intent.extra.shortcut.INTENT", localIntent2); paramContext.sendBroadcast(localIntent1); return; } catch (PackageManager.NameNotFoundException localNameNotFoundException) { while (true) localNameNotFoundException.printStackTrace(); } } }


除了 删除快捷方式。 其他的都测试过。 2.2一下的版本保证可用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值