安卓代码中创建快捷方式
2014-12-03 14:52:24
<span style='white-space:pre'></span>/*** 判断是否已经添加快捷方式* content://com.cyanogenmod.trebuchet.settings/favorites 不同的手机,launcher.db数据库放到地方不同* @return*/private boolean shortcutInScreen() {Cursor cursor = getContentResolver()//content://com.android.launcher/favorites.query(Uri.parse('content://com.cyanogenmod.trebuchet.settings/favorites'),null,'intent like ?',new String[] { '%component=com.jike.superflashlight/.MainActivity%' },null);if (cursor.getCount() > 0) {return true;} else {return false;}}/*** 添加桌面快捷方式按钮** @param view*/public void onClickAddShortcut(View view) {if (!shortcutInScreen()) {Intent installShortcut = new Intent('com.android.launcher.action.INSTALL_SHORTCUT');installShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, '应用的名称');Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);// 启动主窗口Intent flashlightIntent = new Intent();flashlightIntent.setClassName('com.jike.superflashlight','com.jike.superflashlight.MainActivity');flashlightIntent.setAction(Intent.ACTION_MAIN);flashlightIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 快捷图标,添加flashlightIntent意图,打开主界面installShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,flashlightIntent);// 发送广播sendBroadcast(installShortcut);Toast.makeText(this, '已成功将快捷方式添加到桌面', Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, '快捷方式已存在,无法继续添加!', Toast.LENGTH_SHORT).show();}}/*** 移除快捷方式按钮** @param view*/public void onClickRemoveShortcut(View view) {if (shortcutInScreen()) {// 删除快捷图标意图Intent uninstallShortcut = new Intent('com.android.launcher.action.UNINSTALL_SHORTCUT');uninstallShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, '应用的名称');// 启动主界面意图Intent flashlightIntent = new Intent();flashlightIntent.setClassName('com.jike.superflashlight','com.jike.superflashlight.MainActivity');uninstallShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,flashlightIntent);flashlightIntent.setAction(Intent.ACTION_MAIN);flashlightIntent.addCategory(Intent.CATEGORY_LAUNCHER);sendBroadcast(uninstallShortcut);} else {Toast.makeText(this, '没有快捷方式,无法删除!', Toast.LENGTH_SHORT).show();}}</span>
赞 (0)
