0
点赞
收藏
分享

微信扫一扫

Android之在launcher里面动态加载桌面图标

点亮自己的那盏灯 2022-04-14 阅读 111

1、在手机桌面加载图标方式

         1)、动态加载


    Launcher.java
    private void addSourceList() {
            Intent launchIntent = new Intent(this, RcGrpActivity.class);
            launchIntent.setAction(Intent.ACTION_MAIN);
            launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.source_list));
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    ShortcutIconResource.fromContext(this, R.drawable.keycard));
     
            ShortcutInfo shortcut = mModel.infoFromShortcutIntent(this, addIntent);
            shortcut.deletable = false;
            shortcut.titleResource = getResources().getResourceName(R.string.source_list);
            shortcut.presetItemId = getResources().getInteger(R.integer.preset_source_list_icon);
     
            if (LauncherModel.presetItemExists(this, shortcut.presetItemId)) {
                return;
            }
     
            ArrayList<ItemInfo> list = new ArrayList<ItemInfo>();
            list.add(shortcut);
     
            mModel.addAndBindAddedApps(this, list, new ArrayList<AppInfo>(), true);
     
        }
     
    LauncherModel.java
     static boolean presetItemExists(Context context, int presetItemId) {
            final ContentResolver cr = context.getContentResolver();
            Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
                    new String[]{"title"},
                    "presetItemId=?",
                    new String[]{Integer.toString(presetItemId)},
                    null);
            if (c == null) {
                return false;
            }
     
            try {
                return c.moveToFirst();
            } finally {
                c.close();
            }
        }


    优点:不需要平板适配

更多请见:http://www.mark-to-win.com/tutorial/52018.html

举报

相关推荐

0 条评论