类android.os.LocaleList源码实例Demo

下面列出了怎么用android.os.LocaleList的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: FimiX8-RE   文件: LanguageUtil.java
@RequiresApi(api = 24)
public static void changeAppLanguage(Context context, Locale setLocale) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getLocaleByLanguage(setLocale);
    config.locale = locale;
    if (VERSION.SDK_INT >= 24) {
        LocaleList localeList = new LocaleList(new Locale[]{locale});
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
源代码2 项目: candybar-library   文件: LocaleHelper.java
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
 
源代码3 项目: SoloPi   文件: LauncherApplication.java
/**
 * 设置应用默认语言
 */
public void setApplicationLanguage() {
    Resources resources = getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getLanguageLocale();
    config.locale = locale;
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        getApplicationContext().createConfigurationContext(config);
    }
    resources.updateConfiguration(config, dm);
}
 
源代码4 项目: android_9.0.0_r45   文件: EditorInfo.java
/**
 * Used to package this object into a {@link Parcel}.
 *
 * @param dest The {@link Parcel} to be written.
 * @param flags The flags used for parceling.
 */
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(inputType);
    dest.writeInt(imeOptions);
    dest.writeString(privateImeOptions);
    TextUtils.writeToParcel(actionLabel, dest, flags);
    dest.writeInt(actionId);
    dest.writeInt(initialSelStart);
    dest.writeInt(initialSelEnd);
    dest.writeInt(initialCapsMode);
    TextUtils.writeToParcel(hintText, dest, flags);
    TextUtils.writeToParcel(label, dest, flags);
    dest.writeString(packageName);
    dest.writeInt(fieldId);
    dest.writeString(fieldName);
    dest.writeBundle(extras);
    if (hintLocales != null) {
        hintLocales.writeToParcel(dest, flags);
    } else {
        LocaleList.getEmptyLocaleList().writeToParcel(dest, flags);
    }
    dest.writeStringArray(contentMimeTypes);
}
 
源代码5 项目: android_9.0.0_r45   文件: EditorInfo.java
public EditorInfo createFromParcel(Parcel source) {
    EditorInfo res = new EditorInfo();
    res.inputType = source.readInt();
    res.imeOptions = source.readInt();
    res.privateImeOptions = source.readString();
    res.actionLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.actionId = source.readInt();
    res.initialSelStart = source.readInt();
    res.initialSelEnd = source.readInt();
    res.initialCapsMode = source.readInt();
    res.hintText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.packageName = source.readString();
    res.fieldId = source.readInt();
    res.fieldName = source.readString();
    res.extras = source.readBundle();
    LocaleList hintLocales = LocaleList.CREATOR.createFromParcel(source);
    res.hintLocales = hintLocales.isEmpty() ? null : hintLocales;
    res.contentMimeTypes = source.readStringArray();
    return res;
}
 
源代码6 项目: android_9.0.0_r45   文件: TextClassifierImpl.java
private TextClassifierImplNative getNative(LocaleList localeList)
        throws FileNotFoundException {
    synchronized (mLock) {
        localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList;
        final ModelFile bestModel = findBestModelLocked(localeList);
        if (bestModel == null) {
            throw new FileNotFoundException("No model for " + localeList.toLanguageTags());
        }
        if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) {
            Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
            final ParcelFileDescriptor fd = ParcelFileDescriptor.open(
                    new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
            mNative = new TextClassifierImplNative(fd.getFd());
            mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative));
            closeAndLogError(fd);
            mModel = bestModel;
        }
        return mNative;
    }
}
 
源代码7 项目: prayer-times-android   文件: LocaleUtils.java
private static void initLocale(Context c) {
    Crashlytics.setString("lang", Preferences.LANGUAGE.get());
    Crashlytics.setString("digits", Preferences.DIGITS.get());
    Configuration config = new Configuration();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = getLocales();
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
    } else {
        Locale locale = getLocale();
        config.setLocale(locale);
        Locale.setDefault(locale);
    }

    c.getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
    c.getApplicationContext().getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
}
 
源代码8 项目: QuickNote   文件: CommonUtil.java
public static void changeLocalLanguage(Context context, Locale locale) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocale(locale);
        config.setLocales(localeList);
        context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    resources.updateConfiguration(config, dm);//所有的系统都需要调用该API
}
 
源代码9 项目: px-android   文件: LocaleContextWrapper.java
@SuppressWarnings("PMD.AvoidReassigningParameters")
@NonNull
private static Context applyNewLocaleConfig(@NonNull Context context, @NonNull final Locale newLocale) {
    final Configuration newLocaleConfig = context.getResources().getConfiguration();
    final Resources res = context.getResources();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final LocaleList localeList = new LocaleList(newLocale);
        newLocaleConfig.setLocales(localeList);
        newLocaleConfig.setLocale(newLocale);
        context = context.createConfigurationContext(newLocaleConfig);
    } else {
        newLocaleConfig.locale = newLocale;
    }

    Locale.setDefault(newLocale);
    res.updateConfiguration(newLocaleConfig, res.getDisplayMetrics());
    return context;
}
 
源代码10 项目: PicturePicker   文件: PictureLangUtils.java
public static Context setLanguage(Context context, Locale locale) {

        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        config.setLocale(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            return context.createConfigurationContext(config);
        } else {
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            context.getResources().updateConfiguration(config, displayMetrics);
            return context;
        }
    }
 
源代码11 项目: prayer-times-android   文件: LocaleUtils.java
public static Context wrapContext(Context context) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(getLocale());
        LocaleList localeList = getLocales();
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else {
        configuration.setLocale(getLocale());
        context = context.createConfigurationContext(configuration);

    }

    return new ContextWrapper(context);
}
 
源代码12 项目: microMathematics   文件: AppLocale.java
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, Locale newLocale)
{
    final Resources res = context.getResources();
    final Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }
    else
    {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }
    return new ContextWrapper(context);
}
 
源代码13 项目: MHViewer   文件: ContextLocalWrapper.java
public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  Configuration configuration = res.getConfiguration();

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    configuration.setLocale(newLocale);

    LocaleList localeList = new LocaleList(newLocale);
    LocaleList.setDefault(localeList);
    configuration.setLocales(localeList);

    context = context.createConfigurationContext(configuration);

  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    configuration.setLocale(newLocale);
    context = context.createConfigurationContext(configuration);

  } else {
    configuration.locale = newLocale;
    res.updateConfiguration(configuration, res.getDisplayMetrics());
  }

  return new ContextLocalWrapper(context);
}
 
源代码14 项目: AndroidWallet   文件: LocalManageUtil.java
/**
 * 设置语言类型
 */
public static void setApplicationLanguage(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getSetLanguageLocale(context);
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
源代码15 项目: imsdk-android   文件: RNI18nModule.java
private WritableArray getLocaleList() {
  WritableArray array = Arguments.createArray();

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList locales = getReactApplicationContext()
        .getResources().getConfiguration().getLocales();

    for (int i = 0; i < locales.size(); i++) {
      array.pushString(this.toLanguageTag(locales.get(i)));
    }
  } else {
    array.pushString(this.toLanguageTag(getReactApplicationContext()
        .getResources().getConfiguration().locale));
  }

  return array;
}
 
源代码16 项目: KernelAdiutor   文件: BaseActivity.java
public static ContextWrapper wrap(Context context, Locale newLocale) {

        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(newLocale);

            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);

            context = context.createConfigurationContext(configuration);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = newLocale;
            res.updateConfiguration(configuration, res.getDisplayMetrics());
        }

        return new ContextWrapper(context);
    }
 
源代码17 项目: QuickNote   文件: QuickNoteContextWrapper.java
public static Context wrap(Context context, Locale locale) {
    Context newContext = context;
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        newContext = context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
        config.setLocale(locale);
        newContext = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
    }
    return new QuickNoteContextWrapper(newContext);
}
 
源代码18 项目: FirefoxReality   文件: KeyboardWidget.java
private void setDefaultKeyboard() {
    KeyboardInterface keyboard = getKeyboardForLocale(SettingsStore.getInstance(getContext()).getKeyboardLocale());
    if (keyboard != null) {
        handleLanguageChange(keyboard);
        return;
    }

    // If the user has not selected any keyboard, find the best match from system locales.
    LocaleList localeList = getResources().getConfiguration().getLocales();
    String[] supportedLocales = new String[mKeyboards.size()];
    for (int i = 0; i < mKeyboards.size(); ++i) {
        supportedLocales[i] = mKeyboards.get(i).getLocale().toLanguageTag();
    }
    Locale bestMatch = localeList.getFirstMatch(supportedLocales);
    keyboard = getKeyboardForLocale(bestMatch);
    if (keyboard == null) {
        // Fall back to english.
        keyboard = getKeyboardForLocale(Locale.ENGLISH);
    }
    handleLanguageChange(keyboard);
}
 
源代码19 项目: spanner   文件: Spans.java
/**
 * @see android.text.style.LocaleSpan#LocaleSpan(LocaleList)
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public static Span locale(@NonNull final LocaleList localeList) {
    return new Span(new SpanBuilder() {
        @Override
        public Object build() {
            return new LocaleSpan(localeList);
        }
    });
}
 
源代码20 项目: simple-keyboard   文件: EditorInfoCompatUtils.java
public static Locale getPrimaryHintLocale(final EditorInfo editorInfo) {
    if (editorInfo == null) {
        return null;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = editorInfo.hintLocales;
        if (localeList != null && !localeList.isEmpty())
            return localeList.get(0);
    }
    return null;
}
 
源代码21 项目: SoloPi   文件: BaseActivity.java
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
    Resources resources = context.getResources();
    Locale locale = LauncherApplication.getInstance().getLanguageLocale();

    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(locale);
    configuration.setLocales(new LocaleList(locale));
    return context.createConfigurationContext(configuration);
}
 
源代码22 项目: SmartPack-Kernel-Manager   文件: BaseActivity.java
public static ContextWrapper wrap(Context context, Locale newLocale) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    } else {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }
    return new ContextWrapper(context);
}
 
源代码23 项目: AppOpsX   文件: LangHelper.java
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
  Resources resources = context.getResources();
  Locale locale = getLocaleByLanguage(context);

  Configuration configuration = resources.getConfiguration();
  configuration.setLocale(locale);
  configuration.setLocales(new LocaleList(locale));
  return context.createConfigurationContext(configuration);
}
 
private TimeZone getTimeZone() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList locales = context.getResources().getConfiguration().getLocales();
    if (!locales.isEmpty()) {
      Locale locale = locales.get(0);
      return Calendar.getInstance(locale).getTimeZone();
    }
  }
  return Calendar.getInstance().getTimeZone();
}
 
源代码25 项目: cronet   文件: LocaleUtils.java
/**
 * Converts LocaleList object to the comma separated BCP 47 compliant string format.
 *
 * @return a well-formed IETF BCP 47 language tag with language and country code that
 *         represents this locale list.
 */
@TargetApi(Build.VERSION_CODES.N)
public static String toLanguageTags(LocaleList localeList) {
    ArrayList<String> newLocaleList = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
        newLocaleList.add(toLanguageTag(locale));
    }
    return TextUtils.join(",", newLocaleList);
}
 
源代码26 项目: android_9.0.0_r45   文件: InputManagerService.java
@NonNull
private static LocaleList getLocalesFromLanguageTags(String languageTags) {
    if (TextUtils.isEmpty(languageTags)) {
        return LocaleList.getEmptyLocaleList();
    }
    return LocaleList.forLanguageTags(languageTags.replace('|', ','));
}
 
源代码27 项目: android_9.0.0_r45   文件: TextLinks.java
private Request(
        CharSequence text,
        LocaleList defaultLocales,
        TextClassifier.EntityConfig entityConfig,
        boolean legacyFallback,
        String callingPackageName) {
    mText = text;
    mDefaultLocales = defaultLocales;
    mEntityConfig = entityConfig;
    mLegacyFallback = legacyFallback;
    mCallingPackageName = callingPackageName;
}
 
源代码28 项目: 365browser   文件: DateTimePickerDialog.java
private Locale getLocale() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList locales = getConfiguration().getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    return getConfiguration().locale;
}
 
源代码29 项目: android_9.0.0_r45   文件: TextLinks.java
private Request(Parcel in) {
    mText = in.readString();
    mDefaultLocales = in.readInt() == 0 ? null : LocaleList.CREATOR.createFromParcel(in);
    mEntityConfig = in.readInt() == 0
            ? null : TextClassifier.EntityConfig.CREATOR.createFromParcel(in);
    mLegacyFallback = true;
    mCallingPackageName = in.readString();
}
 
源代码30 项目: AndroidMultiLanguage   文件: MultiLanguageUtil.java
public Locale getSysLocale() {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }
    return locale;
}
 
 类所在包
 同包方法