【问题标题】:Create shortcuts in Home screen android在主屏幕android中创建快捷方式
【发布时间】:2017-03-21 06:08:44
【问题描述】:

这个问题听起来可能是重复的,但我没有找到有效的答案:

我已经解决了这些问题:

Android create shortcuts on the home screen

但建议的解决方案不起作用。

我使用了以下在 API 级别 中工作的解决方案

  Intent shortcutIntent = new Intent(context,
                LedgerDetailActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ledgerBean.getName());
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
        addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
        addIntent.putExtra("duplicate", false);
        context.sendBroadcast(addIntent);

添加权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

但上述解决方案并非适用于所有设备,并且有时会提供如下奇怪的功能:

  1. 在某些设备中,例如 Samsung Edge(Android N)无法创建快捷方式
  2. 在我的 Android 7.1 (Android N) 模拟器中,只创建了一个快捷方式

谁能帮帮我,这个功能没有官方文档,请不要与Android N中引入的App快捷方式混淆。我需要主屏幕的快捷方式。

【问题讨论】:

  • 不要从所有不起作用的事情开始,也许您应该从解释您正在尝试做什么开始。仅凭主题是不够的。至少有一个答案完全误解了你。您可能需要提供屏幕截图并解释预期结果。
  • 操作应该是com.android.launcher.action.INSTALL_SHORTCUT。在我测试过的三星设备上,创建快捷方式对我也不起作用。
  • 更新我上面的评论。我使用的是Intent.EXTRA_SHORTCUT_ICONbitmap。这导致了TransactionTooLargeException。我不得不调整位图的大小。

标签: android android-launcher android-shortcut


【解决方案1】:

要先添加快捷方式,您必须在 Android 中添加权限:

<uses-permission
    android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

对于以上 23 个 API,请检查以下链接的运行时权限: Runtime Permission in Android

现在添加快捷方式:

private void createShortcut() {
    //Adding shortcut for SampleActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            SampleActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your App Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.mipmap.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
    getApplicationContext().sendBroadcast(addIntent);
}

如果您发现创建了多个快捷方式来避免这种情况,您可以检查该快捷方式是否已创建:

if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
    addShortcut();
    getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2012-03-22
    • 2018-03-29
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多