首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

自动创建快捷方式两种步骤

2013-11-08 
自动创建快捷方式两种方法1,直接打开添加面板不用用户权限void setshortCut() {??????? Intent addShortcu

自动创建快捷方式两种方法

1,直接打开添加面板不用用户权限void setshortCut() {??????? Intent addShortcut = new Intent();
??????? // 设置快捷方式的名字
??????? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式练习");
??????? // 构建快捷方式中专门的图标
??????? Parcelable icon = Intent.ShortcutIconResource.fromContext(
??????????????? ShortcutTest.this, R.drawable.icon);
??????? // 添加快捷方式图标
??????? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
??????? // 构建快捷方式执行的Intent
??????? Intent mailto = new Intent(this, ShortcutTest.class);
??????? // 添加快捷方式Intent
??????? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto);
??????? // 正常
??????? setResult(RESULT_OK, addShortcut);??? }?2,直接添加 需要添加权限??

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />??


private void addShortcut(){??
??????? Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");??
??????????????
??????? //快捷方式的名称??
??????? shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));??
//??????? shortcut.putExtra("duplicate", false); //不允许重复创建??
??????????????
??????? //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer??
??????? //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序??
?????? // ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());?
????????
????? //? shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));?
??????? shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,ShortcutTest.class));??
??????
??????? //快捷方式的图标??
??????? ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);??
??????? shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);??
??????????????
??????? sendBroadcast(shortcut);??
??? }????shortcut.putExtra("duplicate", false); //表示的为不允许重复设置

热点排行