android - 通过 Intent 在 Android 中创建快捷方式

标签 android android-layout homescreen

<分区>

Possible Duplicate:
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);

我的 list 文件:

<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>

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

如何创建快捷方式?

最佳答案

试试这个:

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

并在 list 中添加此权限:

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

关于android - 通过 Intent 在 Android 中创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6424246/

相关文章:

android - 如何更改android : layout_centerHorizontal = "true" to "false" at runtime

适用于小屏幕的 Android eclipse 图片

java - 通过主屏幕替换技术了解 Android 上的自定义锁实现

android - 如何重新构建默认的 "Launcher"应用程序?

java.lang.NoClassDefFoundError : rx. 主题.PublishSubject

android - 如何从java代码创建EditText数字(有符号和无符号十进制)

java - 带图标的主屏幕快捷方式

java - 使用我自己的 Android 应用程序/apk 作为启动器/主屏幕替换

android - onClick 方法问题

android - 在横向模式下支持不同的屏幕尺寸