java - Android - 应用程序使用应用程序名称而不是 Intent.EXTRA_SHORTCUT_NAME 创建快捷方式

标签 java android shortcut

这在早些时候工作得很好,但现在显然不行了。这是我的代码:

if (options[item].equals("Add to Home Screen")) {
    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName("net.dylang.android.system_sounds_pro", "net.dylang.android.system_sounds_pro.SystemSounds");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("profileName", SystemSounds.prop.getProperty(((String) button.getText()).replaceAll(" ", "-")));
    shortcutIntent.putExtra("profileText", button.getText());

    if (SystemSounds.prop.getProperty("vibrated-ringers").contains(((String) button.getText()).replaceAll(" ", "-"))) 
        shortcutIntent.putExtra("vibratedringsi", "true");
    else
        shortcutIntent.putExtra("vibratedringsi", "false");

    if (SystemSounds.prop.getProperty("vibrated-notifications").contains(((String) button.getText()).replaceAll(" ", "-"))) 
        shortcutIntent.putExtra("vibratednotsi", "true");
    else
        shortcutIntent.putExtra("vibratednotsi", "false");

    Intent addIntent = new Intent();

    Toast.makeText(getApplicationContext(), shortcutIntent.getStringExtra("profileText"), 0).show();

    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutIntent.getStringExtra("profileText"));                           
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(ProfilesPage.this, R.drawable.icon));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    ProfilesPage.this.sendBroadcast(addIntent);
}

我的 Toast 显示正确的快捷方式名称,但当我将其添加到主屏幕时,它是我的应用程序的名称。

谢谢

迪伦

最佳答案

试试这个

    public void createShortCut(){
    // a Intent to create a shortCut
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    //repeat to create is forbidden
    shortcutintent.putExtra("duplicate", false);
    //set the name of shortCut
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SmS ");

    //set icon
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    //set the application to lunch when you click the icon
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , MainActivity.class));
    //sendBroadcast,done
    sendBroadcast(shortcutintent);
}

关于java - Android - 应用程序使用应用程序名称而不是 Intent.EXTRA_SHORTCUT_NAME 创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6738339/

相关文章:

Android TextView 文本在触摸时改变颜色,但它不应该

c# - 是否有调试客户端/服务器应用程序的快捷方式?

powershell - 如何在powershell中遵循快捷方式

Java KeyBindings 对 JPanel 没有反应

java - 确定季度边界日期的更好方法

java - 将时间戳转换为字符串

c - C 中的 IF-ELSE 语句快捷方式

java - 这些类和子类静态 block 何时执行(对于枚举)?

java - 访问/sdcard/udisk 中的文件会返回权限被拒绝。 file.exists() 始终返回 false

android - 运行固定时间的 Activity ,然后开始另一项 Activity