[英]How to change android application name and icon at runtime?
After installing an android app, is it possible to change app icon and name dynamically(at runtime) when you press a button in the app? 安装Android应用程序后,当您按下应用程序中的按钮时,是否可以动态更改应用程序图标和名称(在运行时)?
This is the code so far.., 到目前为止,这是代码。.,
getPackageManager().setComponentEnabledSetting(
new ComponentName("com.example.badgemaste", "com.example.badgemaste.MainActivity"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
try {
getPackageManager().setComponentEnabledSetting(
new ComponentName("com.example.badgemaste", "com.example.badgemaste.MainActivity-One"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
} catch (Exception e) {
//handle
}
And in the manifest file... 在清单文件中...
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name2"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.badgemaste.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:icon="@drawable/ic_laun"
android:label="@string/app_name"
android:name="com.example.badgemaste.MainActivity-One"
android:enabled="true"
android:targetActivity="com.example.badgemaste.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
When I run this code, initially first icon will appear (in drawer and also in action bar) and then after I close the app and re-run it, it will switch to the other icon. 当我运行此代码时,最初将出现第一个图标(在抽屉中以及在操作栏中),然后在我关闭应用程序并重新运行它之后,它将切换到另一个图标。
What I want to do is, assign this procedure to a button, that is if I only press that button, icon will change, otherwise no. 我想要做的是,将此过程分配给一个按钮,即如果我仅按下该按钮,图标将更改,否则更改。 How can I achieve this?? 我该如何实现?
Now that you're able to change the activity using PackageManager, just Create a button and put the code in it's OnClick. 现在,您可以使用PackageManager更改活动了,只需创建一个按钮并将代码放入OnClick中即可。
If this is working, so should that. 如果这行得通,那就应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.