在Android手機上,如QQ、微信當有未讀消息的時候、咱們能夠看到在應用的啓動圖標的右上角會有一個紅色圈圈、且圈圈裏會動態顯示未讀消息的數目,以下圖顯示:
css
那麼該功能是怎麼實現的呢?
在萬能的互聯網搜索和翻閱了大量相關資料、也請教了一些技術羣裏的大咖們。從他們那裏我獲知、提取了一些關鍵詞:第三方控件BadgeView(實現應用內的數字提醒)、快捷圖標、Launcher、反射。
零零碎碎的花費了近一天時間、終於算是弄明白了。寫了個demo測試程序 驗證並自測了一下。 demo效果以下所示:
三星Galaxy S4上測試效果以下:
java
小米手機上測試效果以下:
android
實現原理:
首先咱們要明白 並非應用自己處理對啓動圖標進行修改、圖標的動態修改的過程主要是在Launcher裏面完成的.在應用安裝,更新,卸載的時候,都會有廣播發出,Launcher在LauncherApplication 中註冊廣播,在LauncherModel中處理接收到廣播的消息,從新加載更新應用信息(如:應用圖標、文字等)。可是原生的android系統是並不支持該特性的(及不能經過發送特定的系統廣播 達到動態修改啓動圖標的效果),可是在強大的第三方Android手機廠商(如:三星、小米)的系統源碼深度定製下、經過修改了Launcher源代碼,增長/註冊了新的廣播接收器用來接收應用發送來的未讀消息數廣播,接收到廣播後,系統將未讀消息的數目顯示事件交給Launcher去處理,調用相關方法去重繪應用的icon,最終達到動態更新應用圖標的效果。 git
在瞭解了實現原理以後、咱們大概明白整個流程是這樣的(原生系統除外):
在第三方手機制造商的ROM下、若是修改了Launcher源碼且支持了上面所說的未讀消息數廣播的接收、那麼咱們只要在應用中發送一條能讓系統接收的廣播就能夠在這種設備的手機上實現本篇想要達到的效果。
可是第三方手機制造商們的這種廣播的接收的條件確定是各不相同的、所以最關鍵的就是要知道各手機制造商的這種廣播的Intent接收條件。
幸運的是 在萬能的互聯網上 總能找到你須要的東西,下面封裝了一個工具類 BadgeUtil.java 實現了不一樣手機制造商的未讀消息數目廣播。具體代碼以下: github
import java.lang.reflect.Field; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Build; import android.widget.Toast; /** * 應用啓動圖標未讀消息數顯示 工具類 (效果如:QQ、微信、未讀短信 等應用圖標)<br/> * 依賴於第三方手機廠商(如:小米、三星)的Launcher定製、原生系統不支持該特性<br/> * 該工具類 支持的設備有 小米、三星、索尼【其中小米、三星親測有效、索尼未驗證】 * @author ice_zhengbin@163.com * */ public class BadgeUtil { /** * Set badge count<br/> * 針對 Samsung / xiaomi / sony 手機有效 * @param context The context of the application package. * @param count Badge count to be set */ public static void setBadgeCount(Context context, int count) { if (count <= 0) { count = 0; } else { count = Math.max(0, Math.min(count, 99)); } if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { sendToXiaoMi(context, count); } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) { sendToSony(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) { sendToSamsumg(context, count); } else { Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show(); } } /** * 向小米手機發送未讀消息數廣播 * @param count */ private static void sendToXiaoMi(Context context, int count) { try { Class miuiNotificationClass = Class.forName("android.app.MiuiNotification"); Object miuiNotification = miuiNotificationClass.newInstance(); Field field = miuiNotification.getClass().getDeclaredField("messageCount"); field.setAccessible(true); field.set(miuiNotification, String.valueOf(count == 0 ? "" : count)); // 設置信息數-->這種發送必須是miui 6才行 } catch (Exception e) { e.printStackTrace(); // miui 6以前的版本 Intent localIntent = new Intent( "android.intent.action.APPLICATION_MESSAGE_UPDATE"); localIntent.putExtra( "android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context)); localIntent.putExtra( "android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count)); context.sendBroadcast(localIntent); } } /** * 向索尼手機發送未讀消息數廣播<br/> * 聽說:需添加權限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未驗證] * @param count */ private static void sendToSony(Context context, int count){ String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } boolean isShow = true; if (count == 0) { isShow = false; } Intent localIntent = new Intent(); localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE"); localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);//是否顯示 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",launcherClassName );//啓動頁 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));//數字 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名 context.sendBroadcast(localIntent); } /** * 向三星手機發送未讀消息數廣播 * @param count */ private static void sendToSamsumg(Context context, int count){ String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", count); intent.putExtra("badge_count_package_name", context.getPackageName()); intent.putExtra("badge_count_class_name", launcherClassName); context.sendBroadcast(intent); } /** * 重置、清除Badge未讀顯示數<br/> * @param context */ public static void resetBadgeCount(Context context) { setBadgeCount(context, 0); } /** * Retrieve launcher activity name of the application from the context * * @param context The context of the application package. * @return launcher activity name of this application. From the * "android:name" attribute. */ private static String getLauncherClassName(Context context) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); // To limit the components this Intent will resolve to, by setting an // explicit package name. intent.setPackage(context.getPackageName()); intent.addCategory(Intent.CATEGORY_LAUNCHER); // All Application must have 1 Activity at least. // Launcher activity must be found! ResolveInfo info = packageManager .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER // if there is no Activity which has filtered by CATEGORY_DEFAULT if (info == null) { info = packageManager.resolveActivity(intent, 0); } return info.activityInfo.name; } }
在啓動的Activity中、發送未讀消息數目廣播 和 重置/清除未讀消息數目廣播 的調用以下:
// 發送未讀消息數目廣播:count爲未讀消息數目(int類型) 微信
BadgeUtil.setBadgeCount(getApplicationContext(), count);
// 發送重置/清除未讀消息數目廣播: app
BadgeUtil.resetBadgeCount(getApplicationContext());
資料參考:
http://blog.csdn.net/andylao62/article/details/41794695
http://blog.csdn.net/wx_962464/article/details/37997299
https://github.com/ekinlyw/android-badge
http://www.tuicool.com/articles/JV7vIr 工具
—————————————————————————————————————
若是文章內容對您有幫助, 能夠幫 頂 一下,來支持一下哦!
若是您對文章內容有任何疑問或有更好的看法, 歡迎經過留言或發郵件的方式聯繫我:
ice_zhengbin@163.com 測試