添加快捷方式到Launcher中

要添加shortcut到Launcher中,有两种方式,第一种方式是直接长按程序图标,然后拖动到屏幕上,第二种是发送广播,让launcher接收,进行添加,在Launcher的Manifist中,可以看到一个广播接收器,代码如下:

<receiver
            android:name="com.android.launcher2.InstallShortcutReceiver"
            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
            <intent-filter>
                <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

,顾名思义,肯定是这个了,先看类:

public class InstallShortcutReceiver extends BroadcastReceiver {
    public static final String ACTION_INSTALL_SHORTCUT =
            "com.android.launcher.action.INSTALL_SHORTCUT";

    // A mime-type representing shortcut data
    public static final String SHORTCUT_MIMETYPE =
            "com.android.launcher/shortcut";

    private final int[] mCoordinates = new int[2];

    public void onReceive(Context context, Intent data) {
        if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
            return;
        }

        int screen = Launcher.getScreen();

        if (!installShortcut(context, data, screen)) {
            // The target screen is full, let's try the other screens
            for (int i = 0; i < Launcher.SCREEN_COUNT; i++) {
                if (i != screen && installShortcut(context, data, i)) break;
            }
        }
    }

    private boolean installShortcut(Context context, Intent data, int screen) {
        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);

        if (findEmptyCell(context, mCoordinates, screen)) {
            Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
            if (intent != null) {
                if (intent.getAction() == null) {
                    intent.setAction(Intent.ACTION_VIEW);
                }

                // By default, we allow for duplicate entries (located in
                // different places)
                boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
                if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) {
                    LauncherApplication app = (LauncherApplication) context.getApplicationContext();
                    ShortcutInfo info = app.getModel().addShortcut(context, data,
                            LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0],
                            mCoordinates[1], true);
                    if (info != null) {
                        Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        return false;
                    }
                } else {
                    Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
                            Toast.LENGTH_SHORT).show();
                }

                return true;
            }
        } else {
            Toast.makeText(context, context.getString(R.string.out_of_space),
                    Toast.LENGTH_SHORT).show();
        }

        return false;
    }

    private static boolean findEmptyCell(Context context, int[] xy, int screen) {
        final int xCount = LauncherModel.getCellCountX();
        final int yCount = LauncherModel.getCellCountY();
        boolean[][] occupied = new boolean[xCount][yCount];

        ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
        ItemInfo item = null;
        int cellX, cellY, spanX, spanY;
        for (int i = 0; i < items.size(); ++i) {
            item = items.get(i);
            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
                if (item.screen == screen) {
                    cellX = item.cellX;
                    cellY = item.cellY;
                    spanX = item.spanX;
                    spanY = item.spanY;
                    for (int x = cellX; x < cellX + spanX && x < xCount; x++) {
                        for (int y = cellY; y < cellY + spanY && y < yCount; y++) {
                            occupied[x][y] = true;
                        }
                    }
                }
            }
        }

        return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
    }
}

在onReceiver方法中,将调用了installShortcut方法,在这个方法中,有这些代码:

//添加快捷方式
ShortcutInfo info = app.getModel().addShortcut(context, data,
                            LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0],
                            mCoordinates[1], true);

addshorecut方法会间接地调用LauncherModel的infoFromShortcutIntent方法,关键代码:

ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
    //获取launcher点击快捷方式要启动的activity
        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
       //获取快捷方式显示名称
        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
       //获取快捷方式图标
        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);

 

而在installShortcut方法中,还有一句代码:

 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);

 

这段表示是否可以重复创建快捷方式,默认为true,你可以在发送的intent中进行设置。

 

下面是创建快捷方式的代码:

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intnet = new Intent();
                intnet.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                //下面这段不知道为什么没有作用
//                intnet.setAction(Intent.ACTION_CREATE_SHORTCUT);
                //设置icon图标
                intnet.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.ic_launcher));
                //设置设置快捷方式名称
                intnet.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hah");
                //设置快捷方式要启动的activity
                Intent shrotcutIntent = new Intent(MainActivity.this,MainActivity.class);
                intnet.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shrotcutIntent);
                sendBroadcast(intnet);
            }
        });

 

icon,intent,name,这三项必须进行设置,否则不会创建shortcut

而这一句不知道为什么,不起作用,只有用上边那一段,才会起作用:

intnet.setAction(Intent.ACTION_CREATE_SHORTCUT);

 


加入这段代码则不允许重复创建快捷方式

intnet.putExtra("duplicate", false);

 

至于快捷方式是否创建成功失败什么的提示,系统会完成,无需进行干预

 

posted @ 2013-05-15 21:42  隐没  阅读(583)  评论(0编辑  收藏  举报