问题
I've noticed that the appearience of Dropbox shortcut-like widget to specified folder is consistent with shortcut layout no matter which launcher we use:
So I want to make such a widget that behaves and looks like shortcut and is consistent with user's launcher.
Is it possible to get the shortcut layout of Launcher and use it in our own widget?
回答1:
Aside from AppWidgets
, Android also has a concept of Launcher Shortcuts that are often grouped under the "Widget" label. That Dropbox folder is a Launcher Shortcut.
Shortcuts are simple, all data (icon, label, intent) is static, determined at creation time. They can't be updated dynamically, or resized, or scroll. But they match the launcher styling (and can be placed inside folders or the dock) and use less resources than AppWidget
s.
Sadly they're poorly documented. You can find a sample in the ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java ( https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java ) and references to them at https://developer.android.com/reference/android/content/Intent.html#EXTRA_SHORTCUT_ICON (all the EXTRA_SHORTCUT_... items).
You need an Activity
and AndroidManifest
intent-filter to handle creating the shortcuts:
AndroidManifest.xml
<activity
android:name=".LauncherShortcutActivity" >
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This activity will be called by the launcher with startActivityForResult
, you may present an interface (for example let the user select a folder the shortcut should be to) but ultimately must return an icon, label and intent to the launcher.
void setResult(CharSequence title, int iconResourceId, Intent targetIntent) {
Intent data = new Intent();
data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, iconResourceId));
data.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
setResult(Activity.RESULT_OK, data);
finish();
}
In this case, the icon is specified as a resource of my app. Alternatively the icon can be a bitmap (for example a contact photo):
data.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
回答2:
Is it possible to get the shortcut layout of Launcher and use it in our own widget?
There are hundreds of home screen implementations. Home screen developers can do whatever they want:
- They do not have to support app widgets
- They do not have to support shortcuts
- They do not have to have the ability to launch an app (e.g., single-purpose devices, like kiosks)
- They do not have to show icons for shortcuts or launcher entries
- They do not have to use text captions for shortcuts or launcher entries
- They do not have to have icons and text captions vertically stacked for shortcuts or launcher entries
- Etc.
You are certainly welcome to examine various open source home screen implementations and see how they format their launcher entries. However, there is no universal way of determine if they support shortcuts or launchers, let alone how they format those sorts of entries.
来源:https://stackoverflow.com/questions/25334555/is-it-possible-to-get-the-shortcut-layout-of-launcher-and-use-it-in-our-own-widg