三方应用在Settings里显示
一、在AndroidManifest.xml里添加
<meta-data android:name="com.android.settings.icon"
android:resource="@drawable/xxx" />
二、如果需要在settings里控制三方应用的显示可以在这里过滤
SettingsLib > TileUtils.java
private static void loadActivityTiles(Context context,
UserHandle user, Map<Pair<String, String>, Tile> addedCache,
String defaultCategory, List<Tile> outTiles, Intent intent) {
final PackageManager pm = context.getPackageManager();
final List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
PackageManager.GET_META_DATA, user.getIdentifier());
for (ResolveInfo resolved : results) {
if (!resolved.system) {
// Do not allow any app to add to settings, only system ones.
continue;
}
if ("com.xxx.xxx".equalsIgnoreCase(resolved.activityInfo.packageName)) {//zsg 过滤
if (user.getIdentifier() != UserHandle.USER_SYSTEM) {
Log.i(LOG_TAG, "ignore xxx");
continue;
}
}
final ActivityInfo activityInfo = resolved.activityInfo;
final Bundle metaData = activityInfo.metaData;
loadTile(user, addedCache, defaultCategory, outTiles, intent, metaData, activityInfo);
}
}
三、如果需要只在主用户显示,可以在AndroidManifest.xml中添加如下代码:
<meta-data android:name="com.android.settings.profile"
android:value="primary_profile_only" />
四、在Android 13 上不生效,可能是如下原因
SettingsLib > TileUtils.java
private static void loadTile(UserHandle user, Map<Pair<String, String>, Tile> addedCache,
String defaultCategory, List<Tile> outTiles, Intent intent, Bundle metaData,
ComponentInfo componentInfo) {
// Skip loading tile if the component is tagged primary_profile_only but not running on
// the current user.
if (user.getIdentifier() != ActivityManager.getCurrentUser() //zsg 这里应该判断 user.getIdentifier() != UserHandle.USER_SYSTEM
&& Tile.isPrimaryProfileOnly(componentInfo.metaData)) {
Log.w(LOG_TAG, "Found " + componentInfo.name + " for intent "
+ intent + " is primary profile only, skip loading tile for uid "
+ user.getIdentifier());
return;
}
String categoryKey = defaultCategory;
// Load category
if ((metaData == null || !metaData.containsKey(EXTRA_CATEGORY_KEY))
&& categoryKey == null) {
Log.w(LOG_TAG, "Found " + componentInfo.name + " for intent "
+ intent + " missing metadata "
+ (metaData == null ? "" : EXTRA_CATEGORY_KEY));
return;
} else {
categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
}
final boolean isProvider = componentInfo instanceof ProviderInfo;
final Pair<String, String> key = isProvider
? new Pair<>(((ProviderInfo) componentInfo).authority,
metaData.getString(META_DATA_PREFERENCE_KEYHINT))
: new Pair<>(componentInfo.packageName, componentInfo.name);
Tile tile = addedCache.get(key);
if (tile == null) {
tile = isProvider
? new ProviderTile((ProviderInfo) componentInfo, categoryKey, metaData)
: new ActivityTile((ActivityInfo) componentInfo, categoryKey);
addedCache.put(key, tile);
} else {
tile.setMetaData(metaData);
}
if (!tile.userHandle.contains(user)) {
tile.userHandle.add(user);
}
if (!outTiles.contains(tile)) {
outTiles.add(tile);
}
}