Android 7.1では、デスクトップショートカットを達成するための新しいショートカットの後、iOSの3Dタッチの効果に似ていることができ、アプリケーションへのショートカットを設定するには、長押しデスクトップアイコンは、すぐに対応するターゲットページを入力することができます。
このプロジェクトでは、主にpaypalのようなデスクトップショートカットの動的設定を実装しています。
ショートカット設定のショートカットは、静的設定と動的設定の両方をサポートしています。
ショートカットの静的設定
- icon : ショートカットアイコン
- shortcutId: ショートカット ID。
- shortcutLongLabel および shortcutShortLabel: ショートカット名
- intent: ショートカットをクリックする意図。
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://..///id">
<shortcut
android:enabled="true"
android:icon="@mipmap/icon_scan"
android:shortcutDisabledMessage="@string/shortcutDisabledMessage"
android:shortcutId="scan"
android:shortcutLongLabel="@string/title_scan"
android:shortcutShortLabel="@string/title_scan">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.keno.shortcuts.ScanActivity"
android:targetPackage="com.keno.shortcuts" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/icon_wallet"
android:shortcutDisabledMessage="@string/shortcutDisabledMessage"
android:shortcutId="wallet"
android:shortcutLongLabel="@string/title_wallet"
android:shortcutShortLabel="@string/title_wallet">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.keno.shortcuts.WalletActivity"
android:targetPackage="com.keno.shortcuts" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/icon_contact_phone"
android:shortcutDisabledMessage="@string/shortcutDisabledMessage"
android:shortcutId="contactPhone"
android:shortcutLongLabel="@string/title_contact_phone"
android:shortcutShortLabel="@string/title_contact_phone">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.keno.shortcuts.ContactActivity"
android:targetPackage="com.keno.shortcuts" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts_home" />
全コードは以下の通り:
<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_home" />
</activity>
上記の2つのステップを完了した後、プログラムを実行し、デスクトップアイコンを長押ししてショートカットを表示します。
ショートカットの動的設定、ショートカット設定の追加と削除のサポート
取得の初期化、デフォルト設定方法の設定
ShortcutInfoを構築するには、動的コンフィギュレーションが必要です。 静的コンフィギュレーションでは、以下のパラメータも指定する必要があります。
- icon : ショートカットアイコン
- shortcutId: ショートカット ID。
- shortcutLongLabel および shortcutShortLabel: ショートカット名
- intent: ショートカットをクリックする意図。
//指定した動的メソッドを設定する
shortcutManager.setDynamicShortcuts(shortcutInfoList);
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void initShortcuts() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();
if (shortcutInfoList.isEmpty()) {
shortcutInfoList = new ArrayList<>();
//ダイナミックショートカットの詳細をビルドする
ShortcutInfo shortcutInfoSetting = new ShortcutInfo.Builder(this, ShortcutConfig.SHORTCUT_SETTING)
.setShortLabel(getString(R.string.title_shortcut_setting))
.setLongLabel(getString(R.string.title_shortcut_setting))
.setIcon(Icon.createWithResource(this, R.drawable.ic_settings_black))
.setIntents(new Intent[]{
new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, ShortcutSettingActivity.class)
})
.build();
shortcutInfoList.add(shortcutInfoSetting);
shortcutManager.setDynamicShortcuts(shortcutInfoList);
}
}
設定項目の追加
ShortcutInfoの新しいビルドは、以下のソースコードで追加されます:
/**
* Publish the list of dynamic shortcuts. If there are already dynamic or pinned shortcuts with
* the same IDs, each mutable shortcut is updated.
*
* <p>This API will be rate-limited.
*
* @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
*
* @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
* or when trying to update immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
try {
return mService.addDynamicShortcuts(mContext.getPackageName(),
new ParceledListSlice(shortcutInfoList), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/***
* ショートカット設定を追加する
* @param shortcutModelList
*/
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void addShortcutSetting(List<ShortcutModel> shortcutModelList) {
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
if (shortcutManager == null) {
return;
}
shortcutManager.addDynamicShortcuts(ShortCutBiz.convertToShortcutInfoList(this, shortcutModelList));
}
設定項目の削除
- 一意のフラグ ID を持つ ShortcutInfo を削除します。
/***
* ショートカットの設定を削除する
* @param shortcutModelList
*/
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void removeShortcutSetting(List<ShortcutModel> shortcutModelList) {
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
if (shortcutManager == null) {
return;
}
List<String> shortcutIds = new ArrayList<>();
for (ShortcutModel shortcutModel : shortcutModelList) {
shortcutIds.add(shortcutModel.getId());
}
shortcutManager.removeDynamicShortcuts(shortcutIds);
}