Android应用程序创建桌面快捷方式
16lz
2021-12-04
创建快捷方式代码:
public static final String READ_SETTINGS_PERMISSION = "com.android.launcher.permission.READ_SETTINGS";public static final String INSTALL_SHORTCUT_PERMISSION = "com.android.launcher.action.INSTALL_SHORTCUT";private static final String TAG = "WowUtils";/*** 创建桌面快捷方式* @param context* @param 点击快捷方式进入的Activity* @param title 快捷方式显示名* @param iconRes 快捷方式图标的resource id*/public static void createShortcut(Context context,Class activity,String title,int iconRes){if(context == null || activity == null || isShortcutExist(context)){return ;}Intent addIntent = new Intent(INSTALL_SHORTCUT_PERMISSION);Parcelable icon = Intent.ShortcutIconResource.fromContext(context,iconRes);// 获取快捷键的图标addIntent.putExtra("duplicate", false);Intent myIntent = new Intent(context,activity);addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);// 快捷方式的标题addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作context.sendBroadcast(addIntent);return ;}public static boolean isShortcutExist(Context context) {String shortcutTitle = getShortcutTitle(context);Log.i(TAG, "getShortcutTitle=" + shortcutTitle);return !TextUtils.isEmpty(shortcutTitle);}public static String getShortcutTitle(Context context){if(context == null){return null;}String appLabel = null;try {PackageManager pm = context.getPackageManager();appLabel = pm.getApplicationLabel(pm.getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA)).toString();} catch (Exception e) {return null;}String authority = getAuthorityFromPermission(context,READ_SETTINGS_PERMISSION);Log.i(TAG, "getAuthorityFromPermission=" + authority);final String uriStr = "content://" + authority + "/favorites?notify=true";final Uri uri = Uri.parse(uriStr);final Cursor c = context.getContentResolver().query(uri,new String[] { "title" }, "title=?", new String[] { appLabel },null);if (c != null && c.getCount() > 0) {c.moveToFirst();do{return c.getString(c.getColumnIndexOrThrow("title"));}while(c.moveToNext());}return null;}/*** The launcher is an Application under the Handset Manufacturer* responsibility. The Authority is then not always* "com.android.launcher2.settings". The Handset Manufacturer may rewrite* its own. It can be "com.android.twlauncher" or anything else depending on* the Java package. You need to retrieve the right authority by searching* for a provider that declares the read/write permissions* "com.android.launcher.permission.READ_SETTINGS" or* "com.android.launcher.permission.WRITE_SETTINGS".** @param context* @param permission* @return e.g. com.baidu.launcher2.settings*/public static String getAuthorityFromPermission(Context context, String permission){if (permission == null) return null;List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);if (packs != null) {for (PackageInfo pack : packs) {ProviderInfo[] providers = pack.providers;if (providers != null) {for (ProviderInfo provider : providers) {if (permission.equals(provider.readPermission))return provider.authority;if (permission.equals(provider.writePermission))return provider.authority;}}}}return null;}
Manifest.xml添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
代码使用示例:
WowUtils.createShortcut(this, MainActivity.class, getString(R.string.app_name), R.drawable.ic_launcher);
更多相关文章
- Android(安卓)Studio Gradle project sync failed
- android 电话录音
- android 监听电源键
- android中操纵sqlite数据库
- 《Android高级进阶》— Android(安卓)书籍
- 《第一行代码Android》笔记
- 一、mono for android学习:什么是mono for android(使用C#开发andr
- Android(安卓)NDK 开发教程一:安装NDK
- Android日志系统Logcat源代码简要分析