【问题标题】:Creating shortcuts in Android via Intent [duplicate]通过 Intent 在 Android 中创建快捷方式 [重复]
【发布时间】:2011-06-21 11:03:09
【问题描述】:

可能重复:
Android create shortcuts on the home screen

我的 Activity (HomeActivity.class) 中有一个 TextViewButton

目标:当我点击Button 时,它应该创建一个快捷方式,其中默认的android 图像作为图标和在TextView 中输入的文本。

到目前为止,我发现我们可以使用 ACTION_CREATE_SHORTCUT 创建快捷方式

这是我目前的代码:

Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT);
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
                new Intent(getApplicationContext(), Editor.class));
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText());
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(HomeActivity.this,
                                                        R.drawable.icon));
setResult(RESULT_OK, result);

我的清单文件:

<activity android:name=".HomeActivity" android:label="@string/app_name">

        <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

但是这段代码没有创建任何快捷方式。

如何创建快捷方式?

【问题讨论】:

标签: android android-layout homescreen


【解决方案1】:

试试这个:

Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);

并在清单中添加此权限:

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

【讨论】:

  • 对我不起作用。这还能用吗?
  • @CapDroid:即使存在快捷方式,它也会创建一个快捷方式。我们能否以替换现有快捷方式而不是复制的方式创建快捷方式..?请指导。
  • @CapDroid:这不只是为了股票发射器吗?或者任何用户设置为默认的启动器?
  • 这不适用于 Android 8.0 及更高版本。相反,你需要使用这个:https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat.html#requestPinShortcut(android.content.Context, android.support.v4.content.pm.ShortcutInfoCompat, android.content.IntentSender)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多