Android应用程序创建桌面快捷方式
生活随笔
收集整理的這篇文章主要介紹了
Android应用程序创建桌面快捷方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
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"; /** * 創(chuàng)建桌面快捷方式 * @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添加權(quán)限:<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);
轉(zhuǎn)載于:https://my.oschina.net/oppo4545/blog/197694
總結(jié)
以上是生活随笔為你收集整理的Android应用程序创建桌面快捷方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于heartbeatV2版本的ha-g
- 下一篇: archive for required