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>