Android language 3

Source: Internet
Author: User

[Android] three methods of application language switching

 

Android has done a good job in international and multi-language switching. An application only needs to name the values-[language] folder of the corresponding language family, you can switch between multiple languages by using "Settings"> "Language & keyboard"> "Select language.

But how to implement it in the application? The following methods have been found on the Internet after searching:

View plaincopy to clipboardprint?

 

Resources res = getresources ();

Configuration Config = res. getconfiguration ();

Config. locale = locale;

Displaymetrics dm = res. getdisplaymetrics ();

Res. updateconfiguration (config, DM );

The principle of the first two methods is to implement "Select language" in the application ". By viewing the source code, the core code is:

 

View plaincopy to clipboardprint? Iactivitymanager iactmag = activitymanagernative. getdefault ();

Try {

Configuration Config = iactmag. getconfiguration ();

Config. locale = locale;

// Permission must be declared here: Android. Permission. change_configuration

// Oncreate () will be called again ();

Iactmag. updateconfiguration (config );

} Catch (RemoteException e ){

E. printstacktrace ();

}

PS: Thanks to Zeng Yang for his help.

Iactivitymanager iactmag = activitymanagernative. getdefault ();

Try {

Configuration Config = iactmag. getconfiguration ();

Config. locale = locale;

// Permission must be declared here: Android. Permission. change_configuration

// Oncreate () will be called again ();

Iactmag. updateconfiguration (config );

} Catch (RemoteException e ){

E. printstacktrace ();

}

PS: Thanks to Zeng Yang for his help. It can be found that both iactivitymanager and activitymanagernative are non-public classes. How to call it? The first is API spoofing, and the second is Java reflection.

1. API Spoofing

Android. jar, which is burned to a mobile phone, contains various types and methods required by Android. jar, which is only part of Android. jar, is used by developers. API spoofing refers to simulating undisclosed classes and methods in an application so that the application can compile and generate an APK, however, what is called in the actual operation of the application is still the real android in the mobile phone. jar.

 

The core code shows that the two methods getconfiguration () and updateconfiguration (config) in activitymanagernative are simulated ). With reference to the source code, the engineering structure diagram and Code Simulation of the application are as follows:

 

 

Code:

 

View plaincopy to clipboardprint? Activitymanagernative. Java

Package Android. app;

 

/**

* @ Author sodino E-mail: sodinoopen@hotmail.com

* @ Version time: 11:37:01

*/

Public abstract class activitymanagernative {

Public static iactivitymanager getdefault (){

Return NULL;

}

}

 

Iactivitymanager. Java

Package Android. app;

 

Import Android. content. res. configuration;

Import Android. OS. RemoteException;

 

/**

* @ Author sodino E-mail: sodinoopen@hotmail.com

* @ Version time: 11:37:46

*/

Public abstract interface iactivitymanager {

Public Abstract Configuration getconfiguration () throws RemoteException;

 

Public abstract void updateconfiguration (configuration paramconfiguration)

Throws RemoteException;

}

Activitymanagernative. Java

Package Android. app;

/**

* @ Author sodino E-mail: sodinoopen@hotmail.com

* @ Version time: 11:37:01

*/

Public abstract class activitymanagernative {

Public static iactivitymanager getdefault (){

Return NULL;

}

}

Iactivitymanager. Java

Package Android. app;

Import Android. content. res. configuration;

Import Android. OS. RemoteException;

/**

* @ Author sodino E-mail: sodinoopen@hotmail.com

* @ Version time: 11:37:46

*/

Public abstract interface iactivitymanager {

Public Abstract Configuration getconfiguration () throws RemoteException;

Public abstract void updateconfiguration (configuration paramconfiguration)

Throws RemoteException;

} After simulating these two classes, you can use the core code of the conversion language mentioned above.

 

Directly run the Code:

View plaincopy to clipboardprint?

Private void updatelanguage (locale ){

Log. D ("android_lab", locale. tostring ());

Try {

Object objiactmag, objactmagnative;

Class clziactmag = Class. forname ("android. App. iactivitymanager ");

Class clzactmagnative = Class. forname ("android. App. activitymanagernative ");

Method mtdactmagnative $ getdefault = clzactmagnative. getdeclaredmethod ("getdefault ");

// Iactivitymanager iactmag = activitymanagernative. getdefault ();

Objiactmag = mtdactmagnative $ getdefault. Invoke (clzactmagnative );

// Configuration Config = iactmag. getconfiguration ();

Method mtdiactmag $ getconfiguration = clziactmag. getdeclaredmethod ("getconfiguration ");

Configuration Config = (configuration) mtdiactmag $ getconfiguration. Invoke (objiactmag );

Config. locale = locale;

// Iactmag. updateconfiguration (config );

// Permission must be declared here: Android. Permission. change_configuration

// Oncreate () will be called again ();

Class [] clzparams = {configuration. Class };

Method mtdiactmag $ updateconfiguration = clziactmag. getdeclaredmethod (

"Updateconfiguration", clzparams );

Mtdiactmag $ updateconfiguration. Invoke (objiactmag, config );

} Catch (exception e ){

E. printstacktrace ();

}

}

Private void updatelanguage (locale) {log. D ("android_lab", locale. tostring (); try {object objiactmag, objactmagnative; Class clziactmag = Class. forname ("android. app. iactivitymanager ");
Class clzactmagnative = Class. forname ("android. app. activitymanagernative "); Method mtdactmagnative $ getdefault = clzactmagnative. getdeclaredmethod ("getdefault"); // iactivitymanager iactmag = activitymanagernative. getdefault ();
Objiactmag = mtdactmagnative $ getdefault. Invoke (clzactmagnative); // configuration Config = iactmag. getconfiguration (); Method mtdiactmag $ getconfiguration = clziactmag. getdeclaredmethod ("getconfiguration ");
Configuration Config = (configuration) mtdiactmag $ getconfiguration. invoke (objiactmag); config. locale = locale; // iactmag. updateconfiguration (config); // The permission must be declared here: Android. permission. change_configuration
// The oncreate (); Class [] clzparams = {configuration. Class}; Method mtdiactmag $ updateconfiguration = clziactmag. getdeclaredmethod ("updateconfiguration", clzparams) is called again );
Mtdiactmag $ updateconfiguration. invoke (objiactmag, config);} catch (exception e) {e. after printstacktrace () ;}} was running, it was found that after setting a new locale for the current system, not only did its application language change, but all application languages of the system changed. This must be unreasonable. One solution is to set locale to the system before exiting the application interface. However, I do not like this method. In addition, after calling the updateconfiguration () method, the entire activity will be re-oncreate (), which may be difficult to consider the life cycle of the activity. So there is the third method below.

 

 

The content of values/strings. XML is the same as that of XML/English. xml; the content of values-ZH-RCN/strings. XML is the same as that of XML/Chinese. xml. The reason for this redundancy is that when the APK is generated, all the content in values hits RASC and cannot be read.

The following considerations must be taken into account to achieve the transformation of the language family:

3.1 R. XXXXX. ID corresponds to the text string in the corresponding language family (Special consideration must be given to the R. array. String Array ).

3.2 Parse XML.

3.3 After the language family is set, all interface elements are refreshed manually.

Declare a string in XML in this format:

View plaincopy to clipboardprint?

<String name = "app_name"> language application </string>

<String name = "app_name"> language application </string> the corresponding R file will generate an ID to refer to this string

View plaincopy to clipboardprint?

Public static final class string {

Public static final int app_name = 0x7f050001;

}

Public static final class string {public static final int app_name = 0x7f050001 ;}

The 3.1 problem is how to match the ID and string. The solution is as follows:

View plaincopy to clipboardprint?

Resources res = context. getresources ();

String PKG = context. getpackagename ();

String tag = "app_name ";

Int idtag = res. getidentifier (TAG, "string", PKG );

Resources res = context. getresources (); string PKG = context. getpackagename (); string tag = "app_name"; int idtag = res. getidentifier (TAG, "string", PKG); 3.2 Parse XML

A new tool is used here: xmlresourceparser. The parsing process is a bit winding, but it is simpler than sax. For details, see the code in the languageapp_sodino project.

3.3 manually refresh the interface.

To obtain all the indexes that involve the update components of the language family, you can perform manual work with some effort.

For detailed implementation process, see the following three projects:

Languageapp_apicheat

Languageapp_reflection

Languageapp_sodino

 

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.