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>




 

 
作者:chen.yu
深信服三年半工作经验,目前就职游戏厂商,希望能和大家交流和学习,
微信公众号:编程入门到秃头 或扫描下面二维码
零基础入门进阶人工智能(链接)