我们则进到else中,我们会发现它会启动刚返回的那个activity,所以我们点击创建ApiDemos的shortcut时,它会先启动下这个具有Intent.ACTION_CREATE_SHORTCUT的activity,再退出。接着再返回到Launcher.java中,继续跟下去,completeAddShortcut()方法就是具体的将这个shortcut添加到workspace上的方法,继续跟踪addShortcut()方法到infoFromShortcutIntent()中:
- Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
- String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
- Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
- ..
- Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
4、继续跟踪completeAddShortcut()中的createShortcut()方法,发现shortcut也是个textview,只不过这次是把icon添加到了text的上方,点击这个shortcut触发onclick(),就会启动你的具体功能的activity:
- Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);

下面再看ApiDemos中那个例子,在app/LauncherShortcuts.java中,首先在manifest中:
- <activity-alias Android:name=".app.CreateShortcuts"
- android:targetActivity=".app.LauncherShortcuts"
- android:label="@string/sample_shortcuts">
- <!-- This intent-filter allows your shortcuts to be created in the launcher. -->
- <intent-filter>
- <action android:name="android.intent.action.CREATE_SHORTCUT" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity-alias>
