在Android Lollipop上添加快捷方式

时间:2015-04-18 03:56:01

标签: android android-5.0-lollipop

我尝试使用以下代码向主屏幕添加快捷方式: Intent shortcutIntent = new Intent(CameraSettings.this.getActivity(),                 BackFaceStartServiceActivity.class);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  name);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
             Intent.ShortcutIconResource.fromContext(CameraSettings.this.getActivity(),
                    iconid));
     addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    shortcutUri = addIntent.toUri(Context.MODE_WORLD_WRITEABLE);
    CameraSettings.this.getActivity().sendBroadcast(addIntent);

在棒棒糖下使用Android版本可以正常工作,棒棒糖让它崩溃了:

  java.lang.IllegalArgumentException: Intent must include an explicit package
  name to build an android-app: Intent {  
  act=com.android.launcher.action.INSTALL_SHORTCUT (has extras) }
  at android.content.Intent.toUri(Intent.java:7292) ......

我怎么能解决它?非常感谢。

1 个答案:

答案 0 :(得分:0)

此代码适用于棒棒糖创建快捷方式,更改变量并且您很高兴。

  shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, cnt.getString(R.string.she));
        shortcut.putExtra("duplicate", false);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(cnt.getApplicationContext(), AopebActivity.class));

        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(cnt, R.drawable.share);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        cnt.sendBroadcast(shortcut);