创建应用程序快捷方式主要有以下几种:
1、在launcher的应用程序列表上,长按某一应用程序图标创建快捷方式到桌面
2、在桌面上长按在弹出框中选择快捷方式->应用程序->将添加快捷方式的程序
3、通过程序运行时自动创建
应用程序启动的第一个Activity里添加这样的一个方法:
/** 创建快捷方式 */
public void createShortCut() {
Log.i("coder", "------createShortCut--------");
// 创建快捷方式的Intent
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutIntent.putExtra("duplicate", false);
// 需要实现的名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 定义shortcut点击事件
String action = "com.android.action.test";
Intent respondIntent = new Intent(this, this.getClass());
respondIntent.setAction(action);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
// 点击快捷图片,运行的程序主入口
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
sendBroadcast(shortcutIntent);
}
如果只是添加这些代码,当你卸装你的应用程序的时候你又会发现存在一个问题就是你的应用程序虽然卸载了,可是桌面上的快捷方式并未卸载。其实你只要在你要设置对应启动进入的那个Intent加上这么下面的两个属性就是表明与你的应用绑定了。
// 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
/** 卸载快捷方式 */
void deleteShortcut() {
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//指定要卸载的快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
String action = "com.android.action.test"; //包名
String appClass = this.getPackageName() + "." + this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));
sendBroadcast(shortcut);
}
在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限:
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
判断是否已添加快捷方式:
暂时没有方法能够准确的判断到快捷方式,原因是,
1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content://com.htc.launcher.settings/favorites?notify=true
2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同
提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建
添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" ></uses-permission>
public static boolean hasInstallShortcut(Context context) { boolean hasInstall = false; String AUTHORITY = "com.android.launcher.settings"; int systemversion = Build.VERSION.SDK_INT; Log.i("Build.VERSION.SDK==========>", systemversion + ""); /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/ if(systemversion >= 8){ AUTHORITY = "com.android.launcher2.settings"; } Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor cursor = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?", new String[] { context.getString(R.string.app_name) }, null); if (cursor != null && cursor.getCount() > 0) { hasInstall = true; } return hasInstall; }