要实现在应用程序前台使用不同的图标,而在应用程序后台使用另一个图标,可以使用以下步骤:
res
目录下创建两个不同的图标资源文件,例如:icon_foreground.png
和icon_background.png
。AndroidManifest.xml
文件中的应用程序标签中添加以下属性:
这将设置应用程序的前台图标为icon_foreground.png
。
public class MainActivity extends AppCompatActivity {
private boolean isAppInForeground;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 判断应用程序是否在前台
isAppInForeground = true;
registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
isAppInForeground = true;
updateAppIcon();
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
isAppInForeground = false;
updateAppIcon();
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
}
});
}
private void updateAppIcon() {
if (isAppInForeground) {
setAppIcon(R.mipmap.icon_foreground);
} else {
setAppIcon(R.mipmap.icon_background);
}
}
private void setAppIcon(int resId) {
try {
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
Bundle metaData = activityInfo.metaData;
String appIcon = metaData.getString("com.google.android.gms.wallet.api.enabled");
if (appIcon != null && appIcon.equals("android")) {
int iconRes = getResources().getIdentifier("com.google.android.gms.wallet.api.enabled", "drawable", getPackageName());
if (iconRes != 0) {
resId = iconRes;
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (getApplicationContext().getPackageManager().canRequestPackageInstalls()) {
updateAppIcon(resId);
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getApplicationContext().getPackageName()));
startActivityForResult(intent, REQUEST_INSTALL_UNKNOWN_APP);
}
} else {
updateAppIcon(resId);
}
}
private void updateAppIcon(int resId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager.getDynamicShortcuts().size() > 0) {
ShortcutInfo shortcutInfo = shortcutManager.getDynamicShortcuts().get(0);
ShortcutInfo.Builder builder = new ShortcutInfo.Builder(this, shortcutInfo.getId());
Icon icon = Icon.createWithResource(this, resId);
builder.setIcon(icon);
shortcutManager.updateShortcuts(Arrays.asList(builder.build()));
}
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), MainActivity.class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
try {
Method method = NotificationManager.class.getMethod("setIconBadgeNum", int.class, int.class, PendingIntent.class);
method.invoke(null, getApplicationContext().getPackageManager().getApplicationInfo(getPackageName(), 0).uid, resId, pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这段代码将监听应用程序的生命周期,并在应用程序进入前台或后台时更新应用图标。
AndroidManifest.xml
文件中的
标签中添加以下元数据: