2016-11-01 34 views
0

我的Android 7.1.1新快捷方式存在一些问题。Android动态快捷方式图标

第二个drawable没有资源ID。这里是图片和代码片段。

enter image description here

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)); 
    } 
} 

我在做什么错?

+1

您在有三个应用程序的快捷方式屏幕截图。您的代码被硬连线以始终使用相同的资源。所以,你的第一个和第三个应用程序快捷方式应该有相同的图标......但他们不这样做,更不用说第二个问题了。如果您一直在更改您的应用程序,并且您拥有出色的动态应用程序快捷方式,则可能需要卸载并重新安装应用程序,或者强制您的代码重新构建所有这些动态应用程序快捷方式,然后查看是否有帮助。 – CommonsWare

+0

Nope已经尝试我有2动态和一个静态快捷方式 –

回答

2

现在我找到了解决方法,但我希望他们能在接下来的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)); 
    } 
} 
0

getDynamicShortcuts

在API级别25中添加列表getDynamicShortcuts()返回 来自调用方应用程序的所有动态快捷方式。

此API旨在用于检查当前发布的哪些快捷方式是 。 通过API (如setDynamicShortcuts(List))重新发布返回的ShortcutInfos可能会导致信息丢失如 作为图标。

上述片段为getDynamicShortcuts功能developer.android.com的描述。

所以,它能够更好地使用API​​仅仅为验证或检索的细节,而不是把它放回ShortcutManager

对于进一步的细节,https://developer.android.com/reference/android/content/pm/ShortcutManager.html#getDynamicShortcuts()