更改设备语言时更新通知文本 [英] Updating notification texts when changing device language

查看:159
本文介绍了更改设备语言时更新通知文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我有一个问题.

I'm new at Android development and I have a question.

我正在为用户创建一个通知.没关系的 问题是,当我更改设备语言时,我的通知不会更新您自己.我不知道在Android上是否有任何本地方法可以做到这一点.

I'm creating a notification for the user. That's all OK. The problem is that when I change the device language, my notification doesn't updates yourself. I don't know if there are any native method to do that on Android.

现在,我的解决方案是:创建通知时,我创建一个线程来验证语言是否已更改.如果是这样,它将更新我的通知.当用户清除通知时,线程将停止.

For now, my solution is: when creating the notification, I create a thread that verifies if the language has changed. If so, it updates my notification. The thread is stoped when the user cleans the notification.

所以,我不知道这是不是一个好习惯,或者是否还有其他原因.

So, I don't know if it's a good pratice, or if there's another whay to to that.

PS:该应用程序具有很多string.xml文件来转换字符串.我正在使用Thread类创建线程.

PS: The application has lots of strings.xml files to translate strings. I'm using Thread class to create the thread.

在此先感谢英语不好的人!

Thanks in advance and sry for the bad english!

推荐答案

在您的应用程序类中,您可以保留当前显示的通知ID及其信息的引用.您可以在onConfigurationChanged(Configuration newConfig)中检测语言更改,并在那里更新通知.

In your application class, you can keep reference of the Notification Ids currently showing along with their info. You can detect language change in onConfigurationChanged(Configuration newConfig) and update your notifications there.

public class App extends Application {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //update your notifications there is no need for a Thread
    }
}


另一种解决方案(实际)

为区域更改添加接收器

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //retrieve the list of notifications
        //update them (no need for thread)
    }
}

添加到您的清单:

<receiver android:name="MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.LOCALE_CHANGED">
        </action>
    </intent-filter>
</receiver>

这篇关于更改设备语言时更新通知文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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