问题
How can I change the launcher icon and its label from my application runtime in Android? (if it is possible)
I mean the properties defined in AndroidManifest.xml
: android:icon
and android:label
. I want to replace it by the image I take from the camera.
回答1:
Edit: your original answer didn't specify you wanted to do it at runtime. Below the line is how to do it before it compiles. As for doing it programmatically at runtime:
From this answer:
You cannot change the manifest or the resource in the signed-and-sealed APK, except through a software upgrade.
But also, the accepted answer figured out a "hack" or some cheating way to do it (I presume). I haven't tested it, but there is a source for you to check out.
Or, this link apparently describes how to do this using the "widget" approach.
So lets say your manifest was like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yourPackage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
You would change the launcher icon by putting images into your drawable folders:
Project folder --> res --> all the drawables
Then you would change the android:icon="@drawable/ic_launcher"
to be android:icon="@drawable/whateverTheNameOfTheImageIsYouPutInTheDrawablesFolders"
To change the label:
Open up your strings.xml
file which is located:
Project folder --> res --> values --> strings.xml
And look for the string that looks like this:
<string name="app_name">Your App Name</string>
And just change Your App Name
to be whatever you want the label to be.
Summary:
android:icon="@drawable/ic_launcher"
determines your launcher icon, so just add images to the drawable folders and change ic_launcher
to whatever the name of image is that you want to be the icon.
android:label="@string/app_name"
determines your "label" so just look for app_name
(because the label is referenced to @string/app_name
) in your strings.xml
file and change the contents of app_name
.
回答2:
This entire answer is from this post and it was taken from P-A and CommonsWare.
You cannot change the manifest or the resource in the signed-and-sealed APK, except through a software upgrade. or Try this, it`s work fine for me but not sure for all devices:
Modify your MainActivity section in AndroidManifest.xml, delete from it, line with MAIN category in intent-filter section.
<activity android:name="ru.quickmessage.pa.MainActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" android:label="@string/app_name" android:theme="@style/CustomTheme" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />//DELETE THIS LINE </intent-filter> </activity>
Create
<activity-alias>
for your app, for each of your icons. Like this<activity-alias android:label="@string/app_name" android:icon="@drawable/icon" android:name=".MainActivity-Red" android:enabled="false" android:targetActivity=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
Set programmatically ENABLE attribute for necessary
getPackageManager().setComponentEnabledSetting( new ComponentName("ru.quickmessage.pa", "ru.quickmessage.pa.MainActivity-Red"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Note, At least one must be enabled and above code perfect working up to 4.0 not tested into >4.0.
来源:https://stackoverflow.com/questions/20851274/how-to-change-launcher-icon-and-its-label-from-the-application