导入的依赖
implementation ‘com.umeng.sdk:common:1.5.1’
implementation ‘com.umeng.sdk:analytics:7.5.0’
清单文件
<meta-data android:value="5d3572c53fc19544630000d6" android:name="UMENG_APPKEY"/>
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
defaultConfig {
flavorDimensions "default"
manifestPlaceholders = [qqappid: "100424468"]
applicationId "com.example.ument"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
productFlavors {
Alpha {//自己打包测试使用
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Alpha]
}
Tencent {//投放应用宝市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Tencent]
}
Baidu {//投放百度市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Baidu]
}
Qihoo {//投放360市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Qihoo]
}
Alibaba {//投放阿里巴巴
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Alibaba]
}
Vivo {//投放vivo市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Vivo]
}
Oppo {//投放oppo市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Oppo]
}
Xiaomi {//投放小米市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Xiaomi]
}
Huawei {//投放华为应用市场
manifestPlaceholders = [UMENG_CHANNEL_VALUE: Huawei]
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
}
国际化
//英文资源
<resources>
<string name="app_language">language</string>
</resources>
//中文资源
<resources>
<string name="app_language">语言</string>
</resources>
生成新的Locale对象
public Locale(String language, String country) {
this(language, country, "");
}
//例如,生成English,不限地区的Locale对象
new Locale("en", ")
更改系统的语言设置
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = newLocale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());//更新配置
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLocaleLanguage();
setContentView(R.layout.activity_main);
}
}
更改语言配置需要在MainActivity的setContentView()之前设置
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLocaleLanguage();
setContentView(R.layout.activity_main);
}
}
private void initLocaleLanguage() {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = newLocale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());//更新配置
重启MainActivity
//重启MainActivity
Intent intent = new Intent(SettingActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);