服务通知文本使用系统语言

Service Notification Text Uses System Language

我的应用程序支持两种语言,英语和希伯来语。当我将语言更改为英语并最小化应用程序时,服务通知文本显示为希伯来语而不是英语。 除了通知之外,翻译工作完美。

正如我在标题中提到的,我发现它的发生是因为我的系统语言是希伯来语。

我该如何解决这个问题?

谢谢!

编辑:

更改语言按钮(仅主Activity,从弹出菜单)-

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem menu_item) {
                int itemId = menu_item.getItemId();
                if (itemId == R.id.English) {
                        changeLang(getApplicationContext(), "en");
                        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);


                    finish();
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                } else if (itemId == R.id.Hebrew) {

                        changeLang(getApplicationContext(), "iw");
                        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

                    finish();
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                }
                return true;
            }
        });


    @Override
    protected void attachBaseContext(Context newBase) {

        SharedPreferences preferences = 
  PreferenceManager.getDefaultSharedPreferences(newBase);
        LANG_CURRENT = preferences.getString("Language", "en");

        super.attachBaseContext(MyContextWrapper.wrap(newBase, LANG_CURRENT));
    }

    public void changeLang(Context context, String lang) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("Language", lang);
        editor.apply();
    }

创建服务 -

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (MainActivity.LANG_CURRENT.equals("en")) {
            changeLang(getApplicationContext(),"en");
            Lang = "en";
        } else {
            changeLang(getApplicationContext(),"iw");
            Lang = "iw";
        }
        }

服务通知-

NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch), activityPendingIntent);
NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.stop), servicePendingIntent);


NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setContentTitle(getString(R.string.launch))
        .addAction(action)
        .addAction(action2)
        .setColor(getColor(R.color.foreGroundBackgroundColor))
        .setPriority(NotificationManager.IMPORTANCE_HIGH)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setSmallIcon(R.drawable.applogo)
        .build();

startForeground(2, notification);

除服务通知外,所有活动的翻译都完美无缺..

解决方案感谢@Eyosiyas -

服务Activity-

在 onStartCommand 中我这样做了 -

if (MainActivity.LANG_CURRENT.equals("en")) {
    
    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    Resources resources = this.getResources();
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    resources.updateConfiguration(config, resources.getDisplayMetrics());

} else {
    
    Locale locale = new Locale("iw");
    Locale.setDefault(locale);
    Resources resources = this.getResources();
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    
}

此解决方案仅在您具有 pre-defined 通知字符串时才有效。 喜欢 String.format("Your order id %s has been processed!",orderId);

在您的服务中,绑定通知时meta-data,让服务访问当前选择的语言。根据语言将格式字符串传递给一个单独的方法,returns 翻译标题和消息

onCreate 您服务的方法。 根据需要进行必要的调整。

Locale locale = new Locale(language);
Locale.setDefault(locale);

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());

针对 onStartCommand

的优化代码
Locale locale;
if (MainActivity.LANG_CURRENT.equals("en"))
    locale = new Locale("en");
else
    locale = new Locale("iw");

Locale.setDefault(locale);
Resources resources = this.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());