文章目录
App Shortcuts
App Shortcuts是Android7.1上推出的新功能,可以实现点击Launcher上图标弹出快捷入口:
使用Shortcut
使用App Shortcuts有两种形式,类似广播有动态注册和静态注册,App Shortcuts也有两种形式,分别是动态使用和静态使用。
动态使用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
// android 7.1
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("测试")
.setLongLabel("测试测试")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));
}
}
}
- 通过获取ShortcutManager来动态设置Shortcut.
- 通过build构建一个shortcutInfo对象
- 调用shortcutManager#setDynamicShortcuts更新
下面列出可能会用到的API
方法 | 作用 |
---|---|
setDynamicShortcuts | 更新整个Shortcut列表 |
addDynamicShortcuts | 添加新的条目 |
updateShortcuts | 更新列表 |
removeDynamicShortcuts | 移除指定条目 |
removeAllDynamicShortcuts | 移除全部的条目 |
静态使用
在清单文件入口Activity添加meta标签
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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/shortcuts" />
</activity>
</application>
添加xml目录,创建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="@mipmap/ic_launcher"
android:shortcutId="id1"
android:shortcutLongLabel="@string/app_name"
android:shortcutShortLabel="@string/app_name">
<intent
android:action="android.intent.action_VIEW"
android:targetPackage="com.welcom.shortcut.shortcutdemo">
</intent>
</shortcut>
</shortcuts>