Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android代码在开发中有效,但在play store中失败_Java_Android_Google Play - Fatal编程技术网

Java Android代码在开发中有效,但在play store中失败

Java Android代码在开发中有效,但在play store中失败,java,android,google-play,Java,Android,Google Play,首先,很抱歉英语不好 我正在编写或查找,复制一些代码以更改应用程序语言。当我通过android studio进行构建时,它可以工作。但当我把它推到谷歌游戏商店后,代码失败了 我已经完全记录了它,95%的人确信我的代码中没有bug 第一部分是从stackoverflow复制的 package com.my.package; import android.annotation.TargetApi; import android.app.Activity; import android.conten

首先,很抱歉英语不好

我正在编写或查找,复制一些代码以更改应用程序语言。当我通过android studio进行构建时,它可以工作。但当我把它推到谷歌游戏商店后,代码失败了

我已经完全记录了它,95%的人确信我的代码中没有bug

第一部分是从stackoverflow复制的

package com.my.package;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;

import java.util.Locale;

/**
 * Created by devdeeds.com on 18/4/17.
 * by Jayakrishnan P.M
 */

public class LocaleHelper {
    private static final String DEFAULT_LANGUAGE = "en";
    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static String getLanguage(Context context) {
        Log.i("lang", "getLanguage");

        String lang = getPersistedData(context);
        Log.i("lang", "getLanguage, lang = "+lang);
        return lang;
    }

    public static Context setPrevLanguage(Context context) {
        Log.i("lang", "setPrevLanguage");

        String lang = getLanguage(context);
        Log.i("lang", "setPrevLanguage, prev lang = "+lang);
        return _setLang(context, lang);
    }

    public static Context setLanguage(Context context, String language) {
        Log.i("lang", "setLanguage");
        getPersistedData(context);
        persist(context, language);
        getPersistedData(context);
        return _setLang(context, language);
    }

    private static Context _setLang(Context context, String lang) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, lang);
        }
        return updateResourcesLegacy(context, lang);
    }

    private static String getPersistedData(Context context) {
        Log.i("lang", "getPersistedData");
        Log.i("lang", "getPersistedData, applic ID = "+BuildConfig.APPLICATION_ID);
        SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Activity.MODE_PRIVATE);
        String lang = prefs.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
        Log.i("lang", "getPersistedData, lang = "+lang);
        return lang;
    }

    private static void persist(Context context, String language) {
        SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Resources resources = context.getResources();
        Configuration activityConf = resources.getConfiguration();
        Locale newLocale = new Locale(language);
        activityConf.setLocale(newLocale);
        resources.updateConfiguration(activityConf, resources.getDisplayMetrics());

        Resources applicationRes = context.getApplicationContext().getResources();
        Configuration applicationConf = applicationRes.getConfiguration();
        applicationConf.setLocale(newLocale);
        applicationRes.updateConfiguration(applicationConf, applicationRes.getDisplayMetrics());

        return context.createConfigurationContext(activityConf);

    }

    @SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;

    }
}
第二部分是我如何使用它们

//when changing app language based on previous setting
LocaleHelper.setPrevLanguage(StartActivity.this);
LocaleHelper.setPrevLanguage((HomeActivity)this); 

//when setting app language through Setting/Language
String lang = "fil";  //its an app for fillipino
LocaleHelper.setLanguage(getContext(), lang);
跑步时

LocaleHelper.setPrevLanguage(StartActivity.this);
,它通过android studio和play store版本正确地登录到debuggingbuild。 但只有调试版本才能很好地更改应用程序版本,play store版本不会


是否有人也遇到过这样的问题,相同的代码在调试和播放商店版本中会有不同的工作方式?

这只是一个猜测。如果您正在使用,那么您的应用程序将被拆分以减少下载大小。它实现这一点的方法之一是只提供电话上使用的地区。它检测用户何时通过操作系统更改语言环境并下载额外的字符串。但是当你使用你的代码时,它可能不会发生

要了解这是否是问题所在,您可以尝试使用APK而不是捆绑包,或者不使用