android判断和创建快捷方式(4.03测试通过)
生活随笔
收集整理的這篇文章主要介紹了
android判断和创建快捷方式(4.03测试通过)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
android判斷和創建快捷方式(4.03測試通過)
整理了網上的創建方式的代碼,對于快捷方式的判斷使用系統api獲取當前啟動器來處理,這樣系統定制過或者啟動器不一樣也沒關系 。
一、加權限和聲明目標activity
<!-- 創建快捷方式 --><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <activityandroid:name="com.shortcut.TestActivity"android:configChanges="keyboardHidden|orientation"android:theme="@android:style/Theme.Translucent" ><intent-filter><action android:name="action.com.shortcut.test" /></intent-filter></activity>二、創建代碼
/*** 創建快捷方式* * @param context* @param name 顯示名稱* @param url */public static void createShortCut(Context context, String name, String url) {if (hasShortCut(context, name)) {Log.v("createShortCut", name + "快捷方式已存在");return;}final Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra("duplicate", false);final Parcelable icon = Intent.ShortcutIconResource.fromContext(context,ResourceUtil.getId(context, "drawable", "o2o_game_float_icon"));// 這個參數是啟動的activity的actionfinal Intent targetIntent = new Intent("action.com.shortcut.test");// 目標activitytargetIntent.setClassName(context.getPackageName(),"com.shortcut.TestActivity");targetIntent.putExtra("url", url);targetIntent.putExtra("name", name);targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);context.sendBroadcast(shortcutIntent);}/*** 判斷快捷方式是否已存在* * @param context* @param name* @return*/private static boolean hasShortCut(Context context, String name) {Log.v("LauncherPackageName",getLauncherPackageName(context));String launcherPackage = getLauncherPackageName(context);if (TextUtils.isEmpty(launcherPackage)) {// 查詢不到啟動器時默認已存在快捷方式,不進行創建return true;}// Log.v("LauncherPackageName", launcherPackage);boolean result = false;final String uriStr = "content://" + launcherPackage+ ".settings/favorites?notify=true";final Uri CONTENT_URI = Uri.parse(uriStr);final Cursor c = context.getContentResolver().query(CONTENT_URI, null,"title=?", new String[] { name }, null);if (c != null && c.getCount() > 0) {result = true;}return result;}/*** 獲取正在運行桌面包名(注:存在多個桌面時且未指定默認桌面時,該方法返回"",使用時需處理這個情況)*/private static String getLauncherPackageName(Context context) {final Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);if (res.activityInfo == null) {// should not happen. A home is always installed, isn't it?return "";}if (res.activityInfo.packageName.equals("android")) {// 有多個桌面程序存在,且未指定默認項時;return "";} else {return res.activityInfo.packageName;}}總結
以上是生活随笔為你收集整理的android判断和创建快捷方式(4.03测试通过)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NDK Socket编程:面向连接的通信
- 下一篇: AndroidStudio 如何关闭 I