对于主屏幕快捷方式应用题不会对手机语言的改变而改变 [英] Application title for homescreen shortcut does not change on phone language change

查看:158
本文介绍了对于主屏幕快捷方式应用题不会对手机语言的改变而改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我还设立了一个由code主屏幕快捷方式的应用程序。我的应用程序支持多国语言。当我改变了语言上的手机,应用程序的名称在启动画面正常变化。但该名称不会改变主屏幕快捷方式。在另一方面,如果我先改变语言,然后安装应用程序,在主屏幕上的快捷方式中包含适当的名称。

有人可以帮助我解决这个。

我的主屏幕上的快捷方式code:

  HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
    HomeScreenShortCut.putExtra(重复,假);
    // shortcutIntent中加入addIntent
    意图addIntent =新意图();
    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);
 

解决方案

作为Grimmy提到的快捷方式不会对语言的变化更新itlesf。所以,按照他的建议,我已经写了这解决了我的目的功能。我想与大家分享:

在此功能,当用户启动了第一时间的应用程序,它创建的快捷方式。当用户更改手机语言,然后再次启动应用程序,它首先删除短基于老字号老,然后重新创建使用新名称的快捷方式(注:应用程序名称也更改为语言的变化)。

 公共无效createOrUpdateShortcut(){

    应用程序preferences = preferenceManager.getDefaultShared preferences(本);
    isAppInstalled =约preferences.getBoolean(isAppInstalled,假);

    字符串currentLanguage = Locale.getDefault()getDisplayLanguage()。
    字符串previousSetLanguage =约preferences.getString(phoneLanguage,Locale.getDefault()getDisplayLanguage());

    如果(!previousSetLanguage.equals(currentLanguage)){
        shortcutReinstall = TRUE;
    }

     如果(!isAppInstalled || shortcutReinstall){

        意图HomeScreenShortCut =新的意图(getApplicationContext()
                BrowserLauncherActivity.class);

        HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
        HomeScreenShortCut.putExtra(重复,假);

        如果(shortcutReinstall){
            意图removeIntent =新意图();
            removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,HomeScreenShortCut);
            字符串prevAppName =约preferences.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);
        }

        意图addIntent =新意图();
        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);


        //使preference真
        共享preferences.Editor编辑=应用程序preferences.edit();
        editor.putBoolean(isAppInstalled,真正的);
        editor.putString(phoneLanguage,currentLanguage);
        editor.putString(「APPNAME的getString(R.string.app_name));
        editor.commit();
    }
 

希望它可以帮助别人。要再次感谢grimmy他的指教。

I have an application for which I have also created a home screen shortcut from code. My app supports multiple languages. When I change the language on phone, the app name changes properly in launcher screen. But the name does not change for the home screen shortcut. On other hand, if I first change the language and then install the app, the home screen shortcut contains proper name.

Can someone help me in fixing this.

My home screen shortcut code:

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

解决方案

As Grimmy mentioned that Shortcut doesn't update itlesf on Language change. So, as per his advice, I have written a function which solved my purpose. I would like to share with you all:

In this function, when the user launches the app for 1st time, it creates the shortcut. When the user changes phone language and launches app again, it first deletes the old short based on old name and then recreates the shortcut with the new name (note : app name also changes as language changes).

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

Hope it helps someone. Would again like to thank grimmy for his advise.

这篇关于对于主屏幕快捷方式应用题不会对手机语言的改变而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆