清单文件添加
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:sharedUserId="android.uid.system"
>
1.获取当前语言:
Locale.getDefault().getLanguage();
2.切换语言:
//// 此处需要声明权限:android.permission.CHANGE_CONFIGURATION
//// android:configChanges="keyboardHidden|orientation|screenSize"
//// 会重新调用 onCreate();
try {
Class classActivityManagerNative = Class.forName("android.app.ActivityManagerNative");
Method getDefault = classActivityManagerNative.getDeclaredMethod("getDefault");
Object objIActivityManager = getDefault.invoke(classActivityManagerNative);
Class classIActivityManager = Class.forName("android.app.IActivityManager");
Method getConfiguration = classIActivityManager.getDeclaredMethod("getConfiguration");
Configuration config = (Configuration) getConfiguration.invoke(objIActivityManager);
//new Locale(ZH,"CN");中文
//new Locale(EN,"US");英文
config.setLocale(locale);
Class clzConfig = Class.forName("android.content.res.Configuration");
java.lang.reflect.Field userSetLocale = clzConfig.getField("userSetLocale");
userSetLocale.set(config, true);
Class[] clzParams = {Configuration.class};
Method updateConfiguration = classIActivityManager.getDeclaredMethod("updateConfiguration", clzParams);
updateConfiguration.invoke(objIActivityManager, config);
} catch (Exception e) {
e.printStackTrace();
}
时间相关
添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
1.判断是否是12小时制
DateFormat.is24HourFormat(mContext);
2.判断日期时间是否自动获取
public boolean isDateTimeAuto() {
try {
return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME) > 0;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
}
3.设置时区自动获取,设置时区
android.provider.Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME_ZONE, 1);//自动获取时区
setTimeZone(getDefaultTimeZone());
4.设置12/24小时制
android.provider.Settings.System.putString(mContext.getContentResolver(),
android.provider.Settings.System.TIME_12_24, "12");
android.provider.Settings.System.putString(mContext.getContentResolver(),
android.provider.Settings.System.TIME_12_24, "24");
5.设置系统日期
public void setSysDate(int year,int month,int day){
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
long when = c.getTimeInMillis();
if(when / 1000 < Integer.MAX_VALUE){
((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
6.设置系统时间
public void setSysTime(int hour,int minute){
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long when = c.getTimeInMillis();
if(when / 1000 < Integer.MAX_VALUE){
((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
6.设置系统时区
public void setTimeZone(String timeZone){
final Calendar now = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone(timeZone);
now.setTimeZone(tz);
}
6.获取当前系统时区
public String getDefaultTimeZone(){
return TimeZone.getDefault().getDisplayName();
}
注意清单中权限,后续还需要系统签名文件和打包,安装和卸载命令。