Android in-app language switch implementation

Source: Internet
Author: User

Custom control series is over, make a greatest second act, fill the previous time buried a pit---"in-app language switching." App language change is a configuration, but this configuration and Android phone screen rotation is a little bit the same. When the screen rotates, it destroy the current page and redraws it. And after switching the language will not redraw the page, so this situation caused the smattering small partners on the Internet, not in the curse of the API Pit Dad, is in the hard way to solve the problem. Ideas are very good, the purpose is resolute, but the direction is wrong, the elder brother I also brought into the pit, climbed a long time to climb out of this deep pit. So here today to share the experience, I hope you do not and I step into a hole.

Brief introduction of the reasons for this demand, the previous period of time due to the company's business needs, made an instant communication software. In this app there is a Settings page, which has a "language switching" function, is in the process of using the app, dynamic to switch the language of the app, specific reference settings page . At first to think is very simple thing, is not a simple resource internationalization, strings.xml resource file a whole still not to OK? It's really not such a thing to really move hands, but internationalization is no problem, but how can I change the text resources of all the pages, this is a problem. Below is a description of the next online search for several programs.

First,API spoofing
The Android.jar that was fired into the phone contains all the classes and methods required for Android, and the Android.jar for developers is just one part of it. API spoofing is the practice of simulating an unlisted class and method in an application to let the app compile and build the APK, but the actual operation of the app is still fired to the real Android.jar in the phone.

Second, using the Java reflection mechanism

Iactivitymanager and Activitymanagernative are non-public classes that use Java reflection to invoke methods in them.


But the disadvantage is obvious, both of these methods are to change the language of the system type, the function and you go to set the page to set the language type effect. After discovering that a new locale has been set for the current system, not only has its own application language changed, but all of the system's applications have changed. This must be unreasonable. Perhaps you will say, when exiting the app, the big deal again to change the language back. But in Android, the reasons for killing the app are various, there are memory kills, there are users to kill, sliding side-by-side kill, how can you tell when the app was killed? The following describes my solution, the implementation of the effect is good. Still github code address at the bottom, interested students can go to download to see.

The first step is to use the Android open API to change the configuration while saving the language type

Second step, destroy the current page, redraw to change language type

if your page is not the homepage, you call the above code will only update the current page, the activity on the stack will still not update the interface. such as setting language success, will automatically jump to the homepage, of course, the content of the homepage is also updated, and its implementation is very simple, and we write the same. Is the listening settings successfully, destroy all existing pages, and then jump to the home page. Your app will be able to meet the above requirements, but don't worry about the third step.

The third step, as long as your app is not completely removed from Dalvik or art, is no problem. However, if you reboot, or force the app to kill, it starts again and reverts to the previous language type.

This will let us use the type of language saved in the first step, in the base class baseactivity of all pages, each time the method of the language setting is invoked, so that the page is forced to set the language type of the business requirement. OK, this problem is solved satisfactorily, to add this function for your app, the code changes are very small. Enclosed is the code address: Https://github.com/JackCho/SwitchLanguageDemo

Git address: https://github.com/JackCho/SwitchLanguageDemo.git

If you find it helpful, you are welcome to subscribe to my public account-Android dry sharing . Below is the QR code, to provide you with timely high-quality Android dry.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.