1. 程式人生 > >Android 之應用啟動圖示未讀訊息BadgeNumber動態提醒(如微信 QQ等)

Android 之應用啟動圖示未讀訊息BadgeNumber動態提醒(如微信 QQ等)

public class BadgeUtil {
//注意Field導包為import java.lang.reflect.Field;==坑啊
    /**
     * 實現原理: 首先我們要明白 並不是應用本身處理對啟動圖示進行修改、
     *  圖示的動態修改的過程主要是在Launcher裡面完成的.
     * 在應用安裝,更新,解除安裝的時候,都會有廣播發出, 
     * Launcher在LauncherApplication 中註冊廣播,
     * 在LauncherModel中處理接收到廣播的訊息, 重新載入更新應用資訊
     * (如:應用圖示、文字等)。
     * 但是原生的android系統是並不支援該特性的 
     * (及不能通過傳送特定的系統廣播 達到動態修改啟動圖示的效果),
     * 但是在強大的第三方Android手機廠商(如:三星、小米)
     *  的系統原始碼深度定製下、通過修改了Launcher原始碼,
     * 增加/註冊了新的廣播接收器用來接收應用傳送來的未讀訊息數廣播,
     *  接收到廣播後,系統將未讀訊息的數目顯示事件交給Launcher去處理,
     * 呼叫相關方法去重繪應用的icon,最終達到動態更新應用圖示的效果。
     * 
     * 在瞭解了實現原理之後、我們大概明白整個流程是這樣的(原生系統除外):
     *  在第三方手機制造商的ROM下、如果修改了Launcher原始碼且支援了
     * 上面所說的未讀訊息數廣播的接收、那麼我們只要在應用中傳送一條能讓
     *  系統接收的廣播就可以在這種裝置的手機上實現本篇想要達到的效果。
     * 但是第三方手機制造商們的這種廣播的接收的條件肯定是各不相同的、
     *  因此最關鍵的就是要知道各手機制造商的這種廣播的Intent接收條件。 幸運的是
     * 在萬能的網際網路上 總能找到你需要的東西, 
     * 下面封裝了一個工具類 BadgeUtil.java 實現了不同手機制造商的未讀訊息數目廣播。
     * 具體程式碼如下:
     * 
     */
/** * 對外提供呼叫的方法 * @param context 上下文 * @param count 數量 */ 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; } }