Android程序开发之手机APP创建桌面快捷方式

Rochelle ·
更新时间:2024-09-21
· 620 次阅读

预览效果图:

需要权限:

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

配置文件:AndroidManifest.xml

<activity android:name="com.myself.news.activity.GuideActivity" android:label="@string/title_activity_guide" > <intent-filter> <action android:name="com.myself.news.ACTION_HOME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

在应用的闪屏页面Activity的 oncreate方法调用 installShortcut();

代码:

// 创建快捷方式 // com.android.launcher.permission.INSTALL_SHORTCUT private void installShortcut() { // 判断有没有创建过快捷方式 boolean isCreated = SharedPreferencesUtils.getBoolean(this, GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, false); // 判断是否已经创建过 if (!isCreated) { // 发广播 Intent intent = new Intent(); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 图标 // 根据资源文件id生成Bitmap对象 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory .decodeResource(getResources(), R.drawable.ic_launcher)); // 名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机安全卫士"); // 动作 Intent actionIntent = new Intent(); // 跳到主页面 actionIntent.setAction(GlobalConstantsUtils.ACTION_HOME); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent); sendBroadcast(intent); // 标记已经创建过快捷方式,下次不再创建 SharedPreferencesUtils.setBoolean(this, GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, true); } }

常量工具类GlobalConstantsUtils:

public static final String PREF_IS_SHORTCUT_INTALLED = "is_shortcut_intalled";// 是否已经创建快捷方式 public static final String ACTION_HOME = "com.myself.news.ACTION_HOME";// 跳转到主页面的ACTION


您可能感兴趣的文章:Android应用创建桌面快捷方式代码Android如何创建桌面快捷方式Android添加(创建)、删除及判断是否存在桌面快捷方式的方法解析Android应用启动后自动创建桌面快捷方式的实现方法Android 创建/验证/删除桌面快捷方式(已测试可用)android 为应用程序创建桌面快捷方式技巧分享Android编程实现向桌面添加快捷方式的方法Android编程添加快捷方式(Short)到手机桌面的方法(含添加,删除及查询)Android编程实现创建,删除,判断快捷方式的方法Android应用创建多个快捷方式Android实现向Launcher添加快捷方式的方法Android编程创建桌面快捷方式的常用方法小结【2种方法】



手机app app Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号