我最近创建了很多 B2B 应用程序,并且为客户提供基于每个设备或每个部署自定义应用程序的能力至关重要。
这是我将 App Config 设置添加到您的应用程序的指南,以便任何 MDM 能够通过托管的 Google Play 商店利用这些设置:
清单:你必须在部分中有这个:
<meta-data android:name="android.content.APP_RESTRICTIONS"
android:resource="@xml/app_restrictions" />
app_restrictions.xml 此文件将保存您的应用程序限制。应用程序限制只有几个选项,但您可以通过各种不同的创造性方式使其工作。这是一组基本的限制:
<restrictions xmlns:android="http://schemas.android.com/apk/res/android">
<restriction
android:key="LicenseKey"
android:title="@string/LicenseKey"
android:restrictionType="string"
android:description="@string/LicenseKey_Description"
android:defaultValue="" />
<restriction
android:key="Section1"
android:title="Settings"
android:restrictionType="bundle">
<restriction
android:key="Setting1"
android:title="Setting 1"
android:description="More descriptive description here"
android:restrictionType="choice"
android:entryValues="@array/setting_1_array_values"
android:entries="@array/setting_1_array_choices"
android:defaultValue="@string/setting_1"/>
<restriction
android:key="Setting2"
android:title="Setting 2"
android:description="More descriptive description here"
android:restrictionType="string"
android:defaultValue="Some default string value here" />
<restriction
android:key="Setting3"
android:title="Setting 3"
android:description="More descriptive description here"
android:restrictionType="integer"
android:defaultValue="20" />
</restriction>
<restriction
android:key="Section 2"
android:title="Group things in more sections to make it easier to manage if you have lots of settings"
android:restrictionType="bundle">
<restriction
android:key="Setting4"
android:title="Setting 4"
android:description="More descriptive description here"
android:restrictionType="integer"
android:defaultValue="20" />
<restriction
android:key="Setting5"
android:title="Setting 5"
android:description="More descriptive description here"
android:restrictionType="integer"
android:defaultValue="20" />
</restriction>
对于您的某些限制,您可能需要下拉样式选择器中的预定义项目列表 - 如上所示,您可以为此使用数组并将以下内容放入您的 Strings.xml
字符串.xml:
<string-array name="setting_1_array_choices">
<item>"Choose This for Option A"</item>
<item>"Choose This for Option B"</item>
</string-array>
<string-array name="setting_1_array_values">
<item>option_a</item>
<item>option_b</item>
</string-array>
现在 - 将这些限制加载到您的应用程序中:
RestrictionsManager manager = (RestrictionsManager) getSystemService(this.RESTRICTIONS_SERVICE);
Bundle restrictions = manager.getApplicationRestrictions();
loadRestrictions(restrictions);
为了加载它们,我创建了这个 loadRestrictions 示例:
private void loadRestrictions (Bundle bundle) {
Set<String> keys = bundle.keySet();
if (keys.isEmpty()) {
//empty key set here
//nothing sent via MDM App Config
} else {
//we've got keys to process
for (String k : keys) {
Object value = bundle.get(k);
String valueString = "";
if (value != null) {
valueString = value.toString();
}
switch (k) {
case "Section1":
if (value != null) {
loadRestrictions((Bundle) value);
}
break;
case "Section2":
if (value != null) {
loadRestrictions((Bundle) value);
}
break;
case "Setting1":
//Setting 1
// this is an array of choices
String optionChoice = "";
if(valueString.equals("option_a")) {
optionChoice = "Option A Was Chosen";
} else if (valueString.equals("option_b")) {
optionChoice = "Option B Was Chosen";
} else {
//we should not ever get here?
optionChoice = "ERROR?";
}
break;
case "Setting2":
//Setting 1
// value for this key is in valueString
String setting2 = (String) valueString;
break;
case "Setting3":
//Setting 3
// INTEGER value for this key is in value
int setting3 = (int) value;
break;
case "Setting4":
//Setting 4
// INTEGER value for this key is in value
int setting4 = (int) value;
break;
case "Setting5":
//Setting 5
// INTEGER value for this key is in value
int setting5 = (int) value;
break;
default:
//another odd error occurred here
break;
} //switch
} //for keys
}
}//load restrictions
享受!!!