簡體   English   中英

Android動態快捷方式圖標

[英]Android dynamic shortcuts icons

Android 7.1.1的新快捷方式存在一些問題。

第二個可繪制對象沒有資源ID。 這是圖像和代碼片段。

在此處輸入圖片說明

private void createShortcuts(String deviceValue, String tablequery, int pos, String devImage, int index) {
    ShortcutManager shortcutManager = mActivity.getSystemService(ShortcutManager.class);

    if (index == 0) {

        List<ShortcutInfo> scInfo = shortcutManager.getDynamicShortcuts();

        Bundle b = new Bundle();
        b.putInt("position", pos);
        b.putString("table", tablequery);
        b.putString("device", devImage);

        String add = deviceValue + "_" + tablequery;
        ShortcutInfo shortcut = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

        scInfo.add(shortcut);

        shortcutManager.setDynamicShortcuts(scInfo);
    } else if (index == 1) {
        String remove = deviceValue + "_" + tablequery;
        shortcutManager.removeDynamicShortcuts(Arrays.asList(remove));
    }
}

我究竟做錯了什么?

現在,我找到了一種解決方法,但我希望他們能在下一個API更新中對其進行修復

這是一個看起來不太好的外觀,但可以使用:

private void createShortcuts(String deviceValue, String tablequery, int pos, String devImage, int index) {
    ShortcutManager shortcutManager = mActivity.getSystemService(ShortcutManager.class);
    List<ShortcutInfo> scInfo = shortcutManager.getDynamicShortcuts();

    if (index == 0) {

        Bundle b = new Bundle();
        b.putInt("position", pos);
        b.putString("table", tablequery);
        b.putString("device", devImage);

        String add = deviceValue + "_" + tablequery;

        if (scInfo.size() == 1) {
            ShortcutInfo webShortcut = null, webShortcut1 = null;

            webShortcut = new ShortcutInfo.Builder(mActivity, scInfo.get(0).getId())
                    .setShortLabel(scInfo.get(0).getShortLabel())
                    .setLongLabel(scInfo.get(0).getLongLabel())
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntent(scInfo.get(0).getIntent())
                    .build();

            webShortcut1 = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone_2))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut, webShortcut1));
        } else {
            ShortcutInfo webShortcut = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut));
        }
    } else if (index == 1) {
        String remove = deviceValue + "_" + tablequery;
        shortcutManager.removeDynamicShortcuts(Arrays.asList(remove));
    }
}

getDynamicShortcuts

已在API級別25中添加。List getDynamicShortcuts()從調用者應用返回所有動態快捷方式。

該API旨在用於檢查當前發布了哪些快捷方式。 通過諸如setDynamicShortcuts(List)之類的API重新發布返回的ShortcutInfos可能會導致圖標等信息丟失

上面的片段是用於developer.android.com getDynamicShortcuts功能的描述。

因此,最好只將API用於驗證或檢索詳細信息,而不要在ShortcutManager中重新設置API

有關更多詳細信息, 請https://developer.android.com/reference/android/content/pm/ShortcutManager.html#getDynamicShortcuts()

使用ShortcutManager,我們可以通過以下方式添加和刪除動態應用程序快捷方式:以下方法將創建您的應用程序快捷方式,並且也會將其刪除。

例如。

private void createShortCut(boolean createShortCut)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(MainActivity.this, "id1")
                .setShortLabel("Donate")
                .setLongLabel("Donate to make your contribution")
                .setIcon(Icon.createWithResource(MainActivity.this, R.drawable.ic_icon_new))
                .setIntents(
                        new Intent[]{
                                new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, DonateActivity.class)
                                        .putExtra("fromShortcut", true)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                        })
                .build();

        if (shortcutManager != null) {
            //create shortcuts only if user is logged in otherwise remove it
            if (createShortCut) shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
            else  shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcut.getId()));
        }
    }

}

並在啟動器活動的onCreate()方法中調用此方法。 注意:-這是我動態添加和刪除應用程序快捷菜單的示例代碼。 您可以根據需要自定義它。 在這里,我僅在用戶登錄到應用程序時添加應用程序快捷方式,並在用戶注銷后將其刪除。

          //check if user is logged in or not
        if (AppPreferences.contains("user_id"))
        {
             //user logged in -- create Shortcut
            createShortCut(true);
        }else
         {
            //user logged-out -- remove Shortcut
            createShortCut(false);
         }

將此過濾器添加到您的AndroidManifest.xml文件中“活動”標簽下。 在這里,我的活動名稱是“ DonateActivity”,我可以在其中導航應用快捷菜單的onClick。

<activity
        android:name=".DonateActivity"
        android:label="@string/Donate"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="com.mydonationapp.app.OPEN_DYNAMIC_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM