预览:images/loading.gif' data-original="http://images2015.cnblogs.com/blog/885821/201604/885821-20160417234329082-115228287.png" />
需要权限:
1 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
配置文件:AndroidManifest.
1 <activity2 android:name="com.myself.news.activity.GuideActivity"3 android:label="@string/title_activity_guide" >4 <intent-filter>5 <action android:name="com.myself.news.ACTION_HOME" />6 7 <category android:name="android.intent.category.DEFAULT" />8 </intent-filter>9 </activity>
在应用的闪屏页面Activity的 oncreate方法调用 installShortcut();
代码:
1 // 创建快捷方式 2 // com.android.launcher.permission.INSTALL_SHORTCUT 3 private void installShortcut() { 4 // 判断有没有创建过快捷方式 5 boolean isCreated = SharedPreferencesUtils.getBoolean(this, 6 GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, false); 7 // 判断是否已经创建过 8 if (!isCreated) { 9 // 发广播10 Intent intent = new Intent();11 intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");12 13 // 图标14 // 根据资源文件id生成Bitmap对象15 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory16 .decodeResource(getResources(), R.drawable.ic_launcher));17 // 名称18 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机安全卫士");19 20 // 动作21 Intent actionIntent = new Intent();22 // 跳到主页面23 actionIntent.setAction(GlobalConstantsUtils.ACTION_HOME);24 25 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);26 sendBroadcast(intent);27 28 // 标记已经创建过快捷方式,下次不再创建29 SharedPreferencesUtils.setBoolean(this,30 GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, true);31 }32 }
常量工具类GlobalConstantsUtils:
1 public static final String PREF_IS_SHORTCUT_INTALLED = "is_shortcut_intalled";// 是否已经创建快捷方式
1 public static final String ACTION_HOME = "com.myself.news.ACTION_HOME";// 跳转到主页面的ACTION
原标题:手机APP创建桌面快捷方式
关键词: