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>

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码莎拉蒂 .

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值