gpt4 book ai didi

android - 更改设备语言时更新通知文本

转载 作者:太空狗 更新时间:2023-10-29 15:08:10 24 4
gpt4 key购买 nike

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

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

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

所以,我不知道这是否是一个好的做法,或者是否还有其他方法。

PS:该应用程序有很多 strings.xml 文件来翻译字符串。我正在使用 Thread 类来创建线程。

提前致谢,抱歉英语不好!

最佳答案

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

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

添加到您的 list :

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

关于android - 更改设备语言时更新通知文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com