क्या प्रोग्राम से सीधे एप्लिकेशन आइकन बदलना संभव है?
मेरा मतलब है, फ़ोल्डर icon.pngमें परिवर्तन res\drawable।
मैं उपयोगकर्ताओं को प्रोग्राम से एप्लिकेशन के आइकन को बदलने देना चाहूंगा ताकि अगली बार वे लांचर में पहले से चयनित आइकन को देख सकें।
क्या प्रोग्राम से सीधे एप्लिकेशन आइकन बदलना संभव है?
मेरा मतलब है, फ़ोल्डर icon.pngमें परिवर्तन res\drawable।
मैं उपयोगकर्ताओं को प्रोग्राम से एप्लिकेशन के आइकन को बदलने देना चाहूंगा ताकि अगली बार वे लांचर में पहले से चयनित आइकन को देख सकें।
जवाबों:
यह एक पुराना प्रश्न है, लेकिन अभी भी सक्रिय है क्योंकि कोई स्पष्ट Android सुविधा नहीं है। और फ़ेसबुक के लोगों को चारों ओर एक काम मिला - किसी तरह। आज, मुझे एक रास्ता मिला जो मेरे लिए काम करता है। सही नहीं है (इस उत्तर के अंत में टिप्पणी देखें) लेकिन यह काम करता है!
मुख्य विचार यह है, कि मैं अपने होम स्क्रीन पर लॉन्चर द्वारा निर्मित, अपने ऐप के शॉर्टकट के आइकन को अपडेट करता हूं। जब मैं शॉर्टकट-आइकन पर कुछ बदलना चाहता हूं, तो मैं इसे पहले हटा देता हूं और नए बिटमैप के साथ इसे फिर से बनाता हूं।
यहाँ कोड है। इसका एक बटन है increment। जब दबाया जाता है, तो शॉर्टकट को एक के साथ बदल दिया जाता है जिसमें एक नई गिनती संख्या होती है।
पहले आपको अपने प्रकट में इन दो अनुमतियों की आवश्यकता है:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
फिर आपको शॉर्टकट इंस्टॉल करने और अनइंस्टॉल करने के लिए इस दो तरीकों की आवश्यकता है। shortcutAddविधि उस में एक संख्या के साथ एक बिटमैप पैदा करता है। यह केवल यह प्रदर्शित करने के लिए है कि यह वास्तव में बदलता है। आप शायद उस हिस्से को किसी चीज़ से बदलना चाहते हैं, जिसे आप अपने ऐप में चाहते हैं।
private void shortcutAdd(String name, int number) {
// Intent to be send, when shortcut is pressed by user ("launched")
Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
shortcutIntent.setAction(Constants.ACTION_PLAY);
// Create bitmap with number in it -> very default. You probably want to give it a more stylish look
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(0xFF808080); // gray
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(50);
new Canvas(bitmap).drawText(""+number, 50, 50, paint);
((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap);
// Decorate the shortcut
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
// Inform launcher to create shortcut
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
private void shortcutDel(String name) {
// Intent to be send, when shortcut is pressed by user ("launched")
Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
shortcutIntent.setAction(Constants.ACTION_PLAY);
// Decorate the shortcut
Intent delIntent = new Intent();
delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// Inform launcher to remove shortcut
delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(delIntent);
}
और अंत में, यहां पहले शॉर्टकट को जोड़ने के लिए दो श्रोता हैं और एक इंक्रीमेंट काउंटर के साथ शॉर्टकट को अपडेट करना है।
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.add).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
shortcutAdd("changeIt!", count);
}
});
findViewById(R.id.increment).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
shortcutDel("changeIt!");
count++;
shortcutAdd("changeIt!", count);
}
});
}
टिप्पणियों:
यह तरीका तब भी काम करता है जब आपका ऐप होम स्क्रीन पर अधिक शॉर्टकट्स को नियंत्रित करता है, जैसे अलग-अलग अतिरिक्त में Intent। उन्हें बस अलग-अलग नामों की आवश्यकता है ताकि सही एक की स्थापना रद्द हो जाए और फिर से इंस्टॉल हो जाए।
एंड्रॉइड में शॉर्टकट का प्रोग्राम हैंडलिंग एक प्रसिद्ध, व्यापक रूप से उपयोग किया जाता है, लेकिन आधिकारिक तौर पर समर्थित एंड्रॉइड फीचर नहीं है। यह डिफॉल्ट लॉन्चर पर काम करने लगता है और मैंने इसे कहीं और करने की कोशिश नहीं की। इसलिए मुझे दोष न दें, जब आपको यह उपयोगकर्ता-ईमेल मिलता है "यह मेरे एक्सवाईजेड पर काम नहीं करता है, दोहरी जड़ें, सुपर बर्बाद"
Toastजब कोई शॉर्टकट इंस्टॉल किया गया था और एक शॉर्टकट अनइंस्टॉल होने पर लांचर लिखता है । इसलिए मुझे Toastआइकन बदलने पर हर बार दो एस मिलते हैं । यह सही नहीं है, लेकिन अच्छी तरह से, जब तक कि मेरा बाकी ऐप सही नहीं है ...
यह कोशिश करो, यह मेरे लिए ठीक काम करता है:
1 है। अपने संशोधित MainActivityमें अनुभाग AndroidManifest.xmlके साथ लाइन में, यह से हटाते हैं, MAINमें श्रेणी intent-filterअनुभाग
<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" /> <== Delete this line
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
२। <activity-alias>अपने प्रत्येक आइकन के लिए बनाएं । ऐशे ही
<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>
३। प्रोग्रामेटिक रूप से सेट करें: उपयुक्त के लिए सक्षम विशेषता सेट करेंactivity-alias
getPackageManager().setComponentEnabledSetting(
new ComponentName("ru.quickmessage.pa", "ru.quickmessage.pa.MainActivity-Red"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
ध्यान दें, हर समय कम से कम एक सक्षम होना चाहिए।
आप सॉफ़्टवेयर अपग्रेड के माध्यम से हस्ताक्षरित और सील किए गए एपीके में प्रकट या संसाधन को नहीं बदल सकते।
प्रोग्रामेटिकली, आप एप्लिकेशन लॉन्चर को स्वयं प्रकाशित करना चाह सकते हैं:
नोट: यह विधि अब एंड्रॉइड 8.0 - ओरेओ के साथ काम करना शुरू नहीं करती है
अपने AndroidManifest.xml में, जोड़ें:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
फिर आपको अपना ऐप लॉन्चर आशय बनाने की आवश्यकता है:
Intent myLauncherIntent = new Intent();
myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName");
myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
अपने एप्लिकेशन लॉन्चर और कस्टम आइकन के साथ एक इंस्टॉलेशन शॉर्टकट बनाएं:
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name");
intent.putExtra
(
Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext
(
getApplicationContext(),
R.drawable.app_icon
)
);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
और अंत में प्रसारण आशय का शुभारंभ करें:
getApplicationContext().sendBroadcast(intent);
मान लें कि होम स्क्रीन पर दिखाए गए आइकन को बदलने का मतलब है, यह आसानी से एक विजेट बनाकर किया जा सकता है जो वास्तव में ऐसा करता है। यहां एक लेख है जो प्रदर्शित करता है कि iPhone के समान "नए संदेश" प्रकार के अनुप्रयोग के लिए कैसे पूरा किया जा सकता है:
@ पीए का समाधान आंशिक रूप से मेरे लिए काम करता है। नीचे मेरे निष्कर्षों का विवरण दें:
1) पहला कोड स्निपेट गलत है, नीचे देखें:
<activity
...
<intent-filter>
==> <action android:name="android.intent.action.MAIN" /> <== This line shouldn't be deleted, otherwise will have compile error
<category android:name="android.intent.category.LAUNCHER" /> //DELETE THIS LINE
</intent-filter>
</activity>
2) किसी अन्य को सक्षम करने से पहले सभी आइकन को अक्षम करने के लिए निम्न कोड का उपयोग करना चाहिए, अन्यथा यह प्रतिस्थापित करने के बजाय एक नया आइकन जोड़ देगा।
getPackageManager().setComponentEnabledSetting(
getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
लेकिन, यदि आप ऊपर दिए गए कोड का उपयोग करते हैं, तो होमस्क्रीन पर शॉर्टकट को हटा दिया जाएगा! और यह स्वचालित रूप से वापस नहीं जोड़ा जाएगा। आप प्रोग्राम को वापस आइकन जोड़ने में सक्षम हो सकते हैं, लेकिन यह संभवत: पहले की तरह नहीं रहेगा।
3) ध्यान दें कि आइकन तुरंत बदल नहीं जाएगा, इसमें कई सेकंड लग सकते हैं। यदि आप इसे बदलने के ठीक बाद क्लिक करते हैं, तो आपको यह कहते हुए एक त्रुटि मिल सकती है: "ऐप इंस्टॉल नहीं है"।
तो, IMHO यह समाधान केवल ऐप लॉन्चर में आइकन बदलने के लिए उपयुक्त है, शॉर्टकट के लिए नहीं (यानी होमस्क्रीन पर आइकन)
इस समाधान की कोशिश करो
<activity android:name=".SplashActivity"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias android:label="ShortCut"
android:icon="@drawable/ic_short_cut"
android:name=".SplashActivityAlias"
android:enabled="false"
android:targetActivity=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
जब आप अपना ऐप आइकन बदलना चाहते हैं तो निम्न कोड जोड़ें
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(YourActivity.this,
"your_package_name.SplashActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(YourActivity.this,
"your_package_name.SplashActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
AndroidManifest.xml उदाहरण:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.pritesh.resourceidentifierexample.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:label="RED"
android:icon="@drawable/ic_android_red"
android:name="com.pritesh.resourceidentifierexample.MainActivity-Red"
android:enabled="true"
android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias android:label="GREEN"
android:icon="@drawable/ic_android_green"
android:name="com.pritesh.resourceidentifierexample.MainActivity-Green"
android:enabled="false"
android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias android:label="BLUE"
android:icon="@drawable/ic_android_blue"
android:name="com.pritesh.resourceidentifierexample.MainActivity-Blue"
android:enabled="false"
android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
फिर नीचे दिए गए कोड का पालन करें MainActivity:
ImageView imageView = (ImageView)findViewById(R.id.imageView);
int imageResourceId;
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
int hours = new Time(System.currentTimeMillis()).getHours();
Log.d("DATE", "onCreate: " + hours);
getPackageManager().setComponentEnabledSetting(
getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if(hours == 13)
{
imageResourceId = this.getResources().getIdentifier("ic_android_red", "drawable", this.getPackageName());
getPackageManager().setComponentEnabledSetting(
new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Red"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}else if(hours == 14)
{
imageResourceId = this.getResources().getIdentifier("ic_android_green", "drawable", this.getPackageName());
getPackageManager().setComponentEnabledSetting(
new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Green"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}else
{
imageResourceId = this.getResources().getIdentifier("ic_android_blue", "drawable", this.getPackageName());
getPackageManager().setComponentEnabledSetting(
new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Blue"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
imageView.setImageResource(imageResourceId);
com.pritesh.resourceidentifierexample.MainActivity-Red doesn't exist in com.pritesh.resourceidentifierexampleअपवाद मिल रहा है। यहाँ मैंने आपकी समस्या को प्रदर्शित करने के लिए आपके नाम का उपयोग किया है
मार्कस द्वारा काम कर समाधान प्राप्त करने के लिए मुझे पहले इरादे की आवश्यकता थी:
Intent myLauncherIntent = new Intent(Intent.ACTION_MAIN);
myLauncherIntent.setClassName(this, this.getClass().getName());
myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
उल्लिखित सुझावों को लागू करते हुए, मैंने ऐप के मुद्दे का सामना किया है जब भी डिफ़ॉल्ट आइकन नए आइकन में बदल जाता है। इसलिए कुछ ट्विक्स के साथ कोड लागू किया है। चरण 1)। फ़ाइल में AndroidManifest.xml, Android के साथ डिफ़ॉल्ट गतिविधि के लिए बनाएँ: सक्षम = "सत्य" और Android के साथ अन्य उपनाम: सक्षम = "गलत"। आपके पास नहीं होगा, लेकिन उन लोगों को एंड्रॉइड के साथ जोड़ देगा: सक्षम = "सच"।
<activity
android:name=".activities.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
</activity>
<!-- <activity-alias used to change app icon dynamically> : default icon, set enabled true -->
<activity-alias
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:name=".SplashActivityAlias1" <!--put any random name started with dot-->
android:enabled="true"
android:targetActivity=".activities.SplashActivity"> <!--target activity class path will be same for all alias-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<!-- <activity-alias used to change app icon dynamically> : sale icon, set enabled false initially -->
<activity-alias
android:label="@string/app_name"
android:icon="@drawable/ic_store_marker"
android:roundIcon="@drawable/ic_store_marker"
android:name=".SplashActivityAlias" <!--put any random name started with dot-->
android:enabled="false"
android:targetActivity=".activities.SplashActivity"> <!--target activity class path will be same for all alias-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
चरण 2)। एक ऐसी विधि बनाएं जिसका उपयोग 1 गतिविधि-उपनाम को अक्षम करने के लिए किया जाएगा जिसमें डिफ़ॉल्ट आइकन शामिल है और 2 अन्य नाम सक्षम करें जिसमें आइकन को बदलना होगा।
/**
* method to change the app icon dynamically
*
* @param context
* @param isNewIcon : true if new icon need to be set; false to set default
* icon
*/
public static void changeAppIconDynamically(Context context, boolean isNewIcon) {
PackageManager pm = context.getApplicationContext().getPackageManager();
if (isNewIcon) {
pm.setComponentEnabledSetting(
new ComponentName(context,
"com.example.dummy.SplashActivityAlias1"), //com.example.dummy will be your package
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context,
"com.example.dummy.SplashActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
} else {
pm.setComponentEnabledSetting(
new ComponentName(context,
"com.example.dummy.SplashActivityAlias1"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context,
"com.example.dummy.SplashActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
चरण 3)। अब अपनी आवश्यकता के आधार पर इस विधि को कॉल करें, बटन पर क्लिक करें या तारीख विशिष्ट या अवसर विशिष्ट स्थितियों को कहें, जैसे -
// Switch app icon to new icon
GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, true);
// Switch app icon to default icon
GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, false);
आशा है कि यह उन लोगों की मदद करेगा जो आइकन परिवर्तन पर मारे जाने वाले ऐप के मुद्दे का सामना करते हैं। हैप्पी कोडिंग :)