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

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

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();
        }
    }

优点:不需要平板适配

    2)、静态加载

    通过xml文件加载

     1、xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <favorites xmlns:launcher="http://schemas.android.com/apk/res-auto">
        <shortcut
            launcher:uri="#Intent;action=com.sangfor.action.ACTION_OPEN_RESOURCE;category=android.intent.category.DEFAULT;end"
            launcher:iconRes="@drawable/ic_setting_app"
            launcher:titleRes="@string/source_list"
            launcher:container="-100"
            launcher:screen="0"
            launcher:x="2"
            launcher:y="2"
            launcher:deletable="false"
            launcher:presetItemId="@integer/preset_settings_icon"
            />
    </favorites>

    2、  AndroidManifest.xml

        <activity
            android:name="com.sangfor.vpn.client.phone.resource.RcGrpActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/about"
            android:screenOrientation="behind"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
            <intent-filter>
                <action android:name="com.sangfor.action.ACTION_OPEN_RESOURCE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
相关文章
|
4月前
|
Android开发
Android Launcher研究(二)-----------Launcher为何物,究竟是干什么
Android Launcher研究(二)-----------Launcher为何物,究竟是干什么
70 2
|
4月前
|
Java Android开发
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
591 0
|
4月前
|
Android开发
Android 如何将定制的Launcher成为系统中唯一的Launcher
Android 如何将定制的Launcher成为系统中唯一的Launcher
128 2
|
4月前
|
Android开发
Android 状态栏WiFi图标的显示逻辑
Android 状态栏WiFi图标的显示逻辑
113 0
|
1月前
|
Android开发
解决android apk安装后出现2个相同的应用图标
解决android apk安装后出现2个相同的应用图标
141 2
|
1月前
|
XML Android开发 数据格式
Android实战经验之Kotlin中快速实现动态更改应用图标和名称
本文介绍在Android中通过设置多个活动别名动态更改应用图标和名称的方法,涉及XML配置及Kotlin代码示例。
112 10
|
3月前
|
存储 Android开发
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
42 0
|
4月前
|
存储 Android开发
android launcher总体分析
android launcher总体分析
60 1
|
4月前
|
Java Android开发
Android桌面快捷方式图标生成与删除 使用Intent与launcher交互
Android桌面快捷方式图标生成与删除 使用Intent与launcher交互
81 1
|
4月前
|
Android开发
Android launcher development resources
Android launcher development resources
21 1