image.png
静态快捷方式
只需要通过 AndroidManifest.xml
文件配置即可添加快捷方式
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 指定快捷方式的资源文件 -->
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/fk_shortcuts" />
</activity>
<!-- 额外配置一个Activity -->
<activity android:name=".StaticActivity" />
xml/fk_shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/fklogo"
android:shortcutDisabledMessage="@string/disable_message"
android:shortcutId="fk_shortcut"
android:shortcutLongLabel="@string/long_label"
android:shortcutShortLabel="@string/short_label">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.myapplication.StaticActivity"
android:targetPackage="com.example.myapplication" />
<!-- categories目前只能指定android.shortcut.conversation -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- 下面可根据需要列出多个shortcut元素 -->
</shortcuts>
enabled
设置该快捷项是否可用
icon
设置该快捷项的图标
shortcutDisabledMessage
设置禁用该快捷项时所显示的文本
shortcutId
设置该快捷项的 ID
shortcutLongLabel
设置该快捷项的长标题
shortcutShortLabel
设置该快捷项的短标题
StaticActivity
public class StaticActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static);
}
}
image.png
动态快捷方式
通过 ShortcutManager
可为应用动态添加、删除、更新快捷方式。
public class MainActivity extends AppCompatActivity {
public static final String ID_PREFIX = "FK";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBn = findViewById(R.id.add);
Button deleteBn = findViewById(R.id.delete);
// 获取快捷方式管理器:ShortcutManager
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
// 为按钮的单击事件添加监听器
addBn.setOnClickListener(view -> {
// 获取所有动态添加的快捷方式
List<ShortcutInfo> infList = shortcutManager.getDynamicShortcuts();
// 如果还没有添加动态的快捷方式
if (infList == null || infList.isEmpty()) {
List<ShortcutInfo> addList = new ArrayList<>();
// 采用循环添加4个动态快捷方式
for (int i = 1; i < 5; i++) {
// 为快捷方式创建Intent
Intent intent = new Intent(MainActivity.this, DynamicActivity.class);
// 必须设置action属性
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", "第" + i + "条消息");
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, ID_PREFIX + i)
.setShortLabel("快捷方式" + i)
.setLongLabel("详细描述" + i)
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(intent).build();
addList.add(shortcut);
}
// 添加多个快捷方式
shortcutManager.addDynamicShortcuts(addList);
Toast.makeText(this, "快捷方式添加成功",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "快捷方式已经存在",
Toast.LENGTH_SHORT).show();
}
});
// 为按钮的单击事件添加监听器
deleteBn.setOnClickListener(view -> {
// 获取所有动态添加的快捷方式
List<ShortcutInfo> infList = shortcutManager.getDynamicShortcuts();
// 如果还没有添加动态的快捷方式
if (infList == null || infList.isEmpty()) {
Toast.makeText(this, "还没有添加快捷方式",
Toast.LENGTH_SHORT).show();
} else {
// 删除所有的快捷方式
shortcutManager.removeAllDynamicShortcuts();
Toast.makeText(this, "删除快捷方式成功",
Toast.LENGTH_SHORT).show();
}
});
}
}
DynamicActivity
public class DynamicActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
TextView tv = findViewById(R.id.tv);
tv.setText(getIntent().getExtras().get("msg").toString());
}
}
image.png
桌面快捷方式
通过 ShortcutManager
进行动态添加。
- 使用
ShortcutManager
的isRequestPinShortcutSupported()
方法判断当前 Android 版本是否支持 Pinned 快捷方式(Android 8 才支持)。 - 为 Pinned 快捷方式创建
ShortcutInfo
,该对象同样需要包含ID、图标、长标题、短标题、Intent等。这一步可分为两种情况。
- 如果要创建的
ShortcutInfo
在该应用的快捷项列表中己经存在(根据 ID 判断),系统将可直接使用已有ShortcutInfo
对象的ID、图标、长标题、短标题、Intent 信息。 - 如果要创建的
ShortcutInfo
在该应用的快捷项列表中不存在,则系统必须为ShortcutInfo
对象设置ID、图标、长标题、短标题、Intent 信息。
- 使用
ShortcutManager
的requestPinShortcut()
方法请求添加 Pined 快捷方式。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBn = findViewById(R.id.add);
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
addBn.setOnClickListener(view -> {
if (shortcutManager.isRequestPinShortcutSupported()) {
// 设置该快捷方式启动的Intent
Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
myIntent.setAction("android.intent.action.VIEW");
// 如果ID为fk-shortcut的快捷方式已经存在,则可省略设置Intent、Icon等属性
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(
MainActivity.this, "my-shortcut")
.setShortLabel("Pinned快捷")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(myIntent).build(); // ①
// 请求添加Pinned快捷方式
shortcutManager.requestPinShortcut(pinShortcutInfo, null); // ②
}
});
}
}
image.png
摘抄至《疯狂Android讲义(第4版)》