现在的位置: 首页 > 综合 > 正文

快速开发14之创建快捷方式

2017年05月17日 ⁄ 综合 ⁄ 共 981字 ⁄ 字号 评论关闭

首先在创建快捷方式在在oncreat方法中执行

	installShortCut();

以及加入权限 

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

private SharedPreferences sp;//以防创立多个
	private void installShortCut() {
		boolean shortcut = sp.getBoolean("shortcut", false);
		if(shortcut)
			return;
		Editor editor = sp.edit();
		//发送广播的意图, 大吼一声告诉桌面,要创建快捷图标了
		Intent intent = new Intent();
		intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		//快捷方式  要包含3个重要的信息 1,名称 2.图标 3.干什么事情
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机小卫士");
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
		//桌面点击图标对应的意图。
		Intent shortcutIntent = new Intent();
		shortcutIntent.setAction("android.intent.action.MAIN");
		shortcutIntent.addCategory("android.intent.category.LAUNCHER");
		shortcutIntent.setClassName(getPackageName(), "com.itheima.mobilesafe.SplashActivity");
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		sendBroadcast(intent);
		editor.putBoolean("shortcut", true);
		editor.commit();
	}

抱歉!评论已关闭.