標籤:android 動態表徵圖 未讀訊息提醒 應用表徵圖 launcher
在Android手機上,如QQ、當有未讀訊息的時候、我們可以看到在應用的啟動表徵圖的右上方會有一個紅色圈圈、且圈圈裡會動態顯示未讀訊息的數目,如顯示:
那麼該功能是怎麼實現的呢?
在萬能的互連網搜尋和翻閱了大量相關資料、也請教了一些技術群裡的大咖們。從他們那裡我獲知、提取了一些關鍵詞:第三方控制項BadgeView(實現應用內的數字提醒)、快捷表徵圖、Launcher、反射。
零零碎碎的花費了近一天時間、終於算是弄明白了。寫了個demo測試程式 驗證並自測了一下。 demo效果如下所示:
三星Galaxy S4上測試效果如下:
小米手機上測試效果如下:
實現原理:
首先我們要明白 並不是應用本身處理對啟動表徵圖進行修改、表徵圖的動態修改的過程主要是在Launcher裡面完成的.在應用安裝,更新,卸載的時候,都會有廣播發出,Launcher在LauncherApplication 中註冊廣播,在LauncherModel中處理接收到廣播的訊息,重新載入更新應用資訊(如:應用表徵圖、文字等)。但是原生的android系統是並不支援該特性的(及不能通過發送特定的系統廣播 達到動態修改啟動表徵圖的效果),但是在強大的第三方Android手機廠商(如:三星、小米)的系統源碼深度定製下、通過修改了Launcher原始碼,增加/註冊了新的廣播接收器用來接收應用發送來的未讀訊息數廣播,接收到廣播後,系統將未讀訊息的數目顯示事件交給Launcher去處理,調用相關方法去重繪應用的icon,最終達到動態更新應用表徵圖的效果。
在瞭解了實現原理之後、我們大概明白整個流程是這樣的(原生系統除外):
在第三方手機製造商的ROM下、如果修改了Launcher源碼且支援了上面所說的未讀訊息數廣播的接收、那麼我們只要在應用中發送一條能讓系統接收的廣播就可以在這種裝置的手機上實現本篇想要達到的效果。
但是第三方手機製造商們的這種廣播的接收的條件肯定是各不相同的、因此最關鍵的就是要知道各手機製造商的這種廣播的Intent接收條件。
幸運的是 在萬能的互連網上 總能找到你需要的東西,下面封裝了一個工具類 BadgeUtil.java 實現了不同手機製造商的未讀訊息數目廣播。具體代碼如下:
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 [email protected] * */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);
// 發送重設/清除未讀訊息數目廣播:
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
—————————————————————————————————————
如果文章內容對您有協助, 可以幫 頂 一下,來支援一下哦!
如果您對文章內容有任何疑問或有更好的見解, 歡迎通過留言或發郵件的方式聯絡我:
[email protected]
如需要轉載,請註明出處,謝謝!!
—————————————————————————————————————
Android系統 小米/三星/索尼 應用啟動表徵圖未讀訊息數(BadgeNumber)動態提醒