您的位置:首页 > 移动开发

[置顶]手机APP创建桌面快捷方式

2016-07-27 22:37 337 查看
预览:


需要权限:

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

配置文件:AndroidManifest.xml

1  <activity
2             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, BitmapFactory
16                     .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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: