天天看點

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>      

繼續閱讀