创建动态快捷方式主要依靠 ShortManager 、 ShortcutInfo 和 ShortcutInfo.Builder 这几个类来实现。ShortcutInfo 和 ShortcutInfo.Builder 主要用来构造快捷方式对象, ShortManager 是一个系统服务,用于管理应用快捷方式,ShortManager 可以通过以下方式获取:
public
class
MainActivity
extends
Activity
implements
View
.
OnClickListener
{
private
static
final
String
TAG
=
MainActivity
.
class
.
getSimpleName
();
private
ShortcutManager
mShortcutManager
;
private
ShortcutInfo
[]
mShortcutInfos
;
@
Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_main
);
//这里常量Context.SHORTCUT_SERVICE会报错,不用管,可正常编译。看着烦的话把minSdkVersion改为25即可
mShortcutManager
=
(
ShortcutManager
)
getSystemService
(
Context
.
SHORTCUT_SERVICE
);
mShortcutInfos
=
new
ShortcutInfo
[]{
getShoppingShortcut
(),
getDateShortcut
()};
findViewById
(
R
.
id
.
btn_set
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_add
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_update
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_disabled
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_remove
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_removeAll
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_print_max_shortcut_per_activity
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_print_dynamic_shortcut
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_print_static_shortcut
).
setOnClickListener
(
this
);
}
private
ShortcutInfo getAlarmShortcut
(
String