Updating notification texts when changing device language

穿精又带淫゛_ 提交于 2019-12-10 19:14:21

问题


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

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: 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!


回答1:


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

Another Solution (Real one)

Add Receiver for locale Change

public class MyReceiver extends BroadcastReceiver {

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

Add to your manifest:

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



回答2:


Most likely I would not have understood your question correctly.

Since you has lots of strings.xml files to translate strings.
You only need to put them in the appropriate folders:

res/values-pt-rPT //For Portuguese Portugal
res/values-fr-rFR // For French France
res/values-fr-rCA // For French France Canada

And so on.



来源:https://stackoverflow.com/questions/19712036/updating-notification-texts-when-changing-device-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!