主屏幕快捷方式的应用程序标题在手机语言更改时不会更改

Sus*_*hil 5 android android-intent

我有一个应用程序,我也从代码创建了一个主屏幕快捷方式.我的应用支持多种语言.当我在手机上更改语言时,应用程序名称会在启动器屏幕中正确更改.但主屏幕快捷方式的名称不会更改.另一方面,如果我首先更改语言然后安装应用程序,主屏幕快捷方式包含正确的名称.

有人可以帮我解决这个问题.

我的主屏幕快捷键代码:

    HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
    HomeScreenShortCut.putExtra("duplicate", false);
    //shortcutIntent is added with addIntent
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    getApplicationContext().sendBroadcast(addIntent);
Run Code Online (Sandbox Code Playgroud)

Sus*_*hil 1

正如 Grimmy 提到的,快捷方式不会在语言更改时更新它。因此,根据他的建议,我编写了一个函数来解决我的目的。我想和大家分享一下:

在此功能中,当用户第一次启动应用程序时,它会创建快捷方式。当用户更改手机语言并再次启动应用程序时,它首先根据旧名称删除旧的快捷方式,然后使用新名称重新创建快捷方式(注意:应用程序名称也会随着语言的变化而变化)。

public void createOrUpdateShortcut() {

    appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);

    String currentLanguage = Locale.getDefault().getDisplayLanguage();
    String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());

    if (!previousSetLanguage.equals(currentLanguage)) {
        shortcutReinstall = true;
    }

     if(!isAppInstalled || shortcutReinstall){

        Intent HomeScreenShortCut= new Intent(getApplicationContext(),
                BrowserLauncherActivity.class);

        HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
        HomeScreenShortCut.putExtra("duplicate", false);

        if(shortcutReinstall) {
            Intent removeIntent = new Intent();
            removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
            String prevAppName = appPreferences.getString("appName", getString(R.string.app_name));
            removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, prevAppName);
            removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
            getApplicationContext().sendBroadcast(removeIntent);
        }

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
        getApplicationContext().sendBroadcast(addIntent);


        //Make preference true
        SharedPreferences.Editor editor = appPreferences.edit();
        editor.putBoolean("isAppInstalled", true);
        editor.putString("phoneLanguage", currentLanguage);
        editor.putString("appName", getString(R.string.app_name));
        editor.commit();
    }
Run Code Online (Sandbox Code Playgroud)

希望它能帮助某人。再次感谢格里米的建议。