动态更改应用程序图标,使应用程序在Android模块React Native中自动关闭



在我的场景中,我使用以下代码动态更新应用程序图标,但每当我更新图标时,我都会关闭应用程序,但图标会更新。如何防止应用程序关闭。

我的代码

@ReactMethod
public void changeIcon(String enableIcon) {
final Activity activity = getCurrentActivity();
PackageManager pm = activity.getApplicationContext().getPackageManager();
System.out.println(activity.getComponentName());
//Remove old icons
pm.setComponentEnabledSetting(activity.getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
//Display new icons
pm.setComponentEnabledSetting(new ComponentName(activity.getBaseContext(),enableIcon),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}

不要启用/禁用活动。制作活动别名。您可以为每个别名指定不同的标签(显示为应用程序名称(和图标。您必须为每个别名指定不同的名称(用于引用(。

<activity android:name=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter
</activity>
<activity-alias
android:label="Red app"
android:icon="@mipmap/icon_red"
android:name=".RED"
android:enabled="true"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:label="Blue app"
android:icon="@mipmap/icon_blue"
android:name=".BLUE"
android:enabled="true"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:label="Green app"
android:icon="@mipmap/icon_green"
android:name=".GREEN"
android:enabled="true"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>

通过启用禁用这些别名来切换图标。如果你做enum会更好,这会让事情变得清楚。使用此代码在不同的活动别名之间切换:

科特林

enum class ICON_COLOUR { RED, BLUE, GREEN }
private fun setIcon(targetColour: ICON_COLOUR) {
for (value in ICON_COLOUR.values()) {
val action = if (value == targetColour) {
PackageManager.COMPONENT_ENABLED_STATE_ENABLED
} else {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
}
packageManager.setComponentEnabledSetting(
ComponentName(BuildConfig.APPLICATION_ID, "${BuildConfig.APPLICATION_ID}.${value.name}"),
action, PackageManager.DONT_KILL_APP
)
}
}

JAVA(枚举版本(

private enum ICON_COLOUR {
RED("RED"), 
GREEN("GREEN"), 
BLUE("BLUE"), 

private String color;

Environment(String color) {
this.color = color;
}

public String getColor() {
return color;
}
}
private void setIcon(ICON_COLOUR targetColour) {
int action;
for (ICON_COLOUR value : ICON_COLOUR.values()) {
if (value == targetColour)
action = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
else
action = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

packageManager.setComponentEnabledSetting(
new ComponentName(BuildConfig.APPLICATION_ID, value.getColor()),
action, PackageManager.DONT_KILL_APP);
}
}

JAVA(字符串数组版本(

private String []iconColour = {"RED", "GREEN",  "BLUE"};
private void setIcon(String targetColour) {
int action;
for (String value : iconColour) {
if (value.equals(targetColour))
action = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
else
action = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

packageManager.setComponentEnabledSetting(
new ComponentName(BuildConfig.APPLICATION_ID, value),
action, PackageManager.DONT_KILL_APP);
}
}

相关内容

  • 没有找到相关文章

最新更新