今天遇到了很無語很坑的需求,說是要在應用安裝的時候根據參數的不同動態生成桌面圖標,這再android里基本上是不可能的,下面有stackoverflow上的一句話:
You cannot change the application icon (or the Android manifest, or any other application resource for that matter) once your APK is compiled. The only way to do this is by re-compiling and pushing an update to the market。也就是說應用打包好之后幾乎是沒有辦法去改變它的圖標的,更別說是動態生成了!
這個問題今天也是困擾了我很久,真的是找不到做法,后來有人提示了下可以用快捷方式,慢慢的嘗試終於能偽裝的把這蛋疼的需求實現了。
首先看看如何為應用創建的快捷方式,刪除快捷方式,以及判斷某個快捷方式是否已經存在。
1 //新增快捷方式 2 private void shortcutAdd(String name, int number) { 3 //設置快捷方式點擊后要打開的Activity(主入口) 4 Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); 5 shortcutIntent.setAction(Constant.ACTION_PLAY); 6 7 //這里創建了一個numbe的bitmap, 也可以設置自己想要的圖表 8 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 9 Paint paint = new Paint(); 10 paint.setColor(0xFF808080); // gray 11 paint.setTextAlign(Paint.Align.CENTER); 12 paint.setTextSize(50); 13 new Canvas(bitmap).drawText(""+number, 50, 50, paint); 14 15 //設置快捷方式 16 Intent addIntent = new Intent(); 17 addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 18 addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); 19 addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap); 20 21 //創建快捷方式 22 addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 23 getApplicationContext().sendBroadcast(addIntent); 24 } 25 //刪除快捷方式 26 private void shortcutDel(String name) { 27 // Intent to be send, when shortcut is pressed by user ("launched") 28 Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); 29 shortcutIntent.setAction(Constant.ACTION_PLAY); 30 31 // Decorate the shortcut 32 Intent delIntent = new Intent(); 33 delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 34 delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); 35 36 // Inform launcher to remove shortcut 37 //刪除快捷方式 38 delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 39 getApplicationContext().sendBroadcast(delIntent); 40 } 41 42 //判斷快捷方式是否存在 43 public boolean isAddShortCut() { 44 45 final ContentResolver cr = this.getContentResolver(); 46 47 int versionLevel = android.os.Build.VERSION.SDK_INT; 48 String AUTHORITY = "com.android.launcher2.settings"; 49 50 //2.2以上的系統的文件文件名字是不一樣的 51 if (versionLevel >= 8) { 52 AUTHORITY = "com.android.launcher2.settings"; 53 } else { 54 AUTHORITY = "com.android.launcher.settings"; 55 } 56 57 final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY 58 + "/favorites?notify=true"); 59 Cursor c = cr.query(CONTENT_URI, 60 new String[] { "title", "iconResource" }, "title=?", 61 new String[] { getString(R.string.app_name) }, null); 62 63 if (c != null && c.getCount() > 0) { 64 return true; 65 } 66 return false; 67 }
不要忘記加相應的權限設置:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
以上就是如何為應用創建、刪除應用快捷方式了,但是需求上是想要動態設置圖標,僅僅添加快捷方式的話,原來的應用圖標還是在的,我們也可以設置應用圖標不顯示的。
一下就是如何隱藏應用圖標
只要在清單文件中的<intent-filter>的節點下設置
<data android:host="MainActivity" android:scheme="com.android.example" /> 即可。
<activity android:name="com.longtime.ajy.MainActivity" android:screenOrientation="portrait" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <!--data android:host="MainActivity" android:scheme="com.android.example" / --> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
關於為什么這樣設置就不顯示圖標:http://blog.csdn.net/xiazdong/article/details/7764865 這里有很好的說明,感興趣看看。