国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 桌面圖標右上角顯示未讀消息數字

2019-12-12 03:08:21
字體:
來源:轉載
供稿:網友

背景:

在Android原生系統中,眾所周知不支持桌面圖標顯示未讀消息提醒的數字,雖然第三方控件BadgeView可以實現應用內的數字提醒。但對于系統的圖標,特別是app的logo圖標很難實現數字標志,即使是繪圖的方式不斷修改,但這種方式天生弊端,實用性很差。但幸運的是,一些強大的手機廠商(小米,三星,索尼)提供了私有的API,但也帶來了難度,API的不同就意味著代碼量的增加和兼容性問題更加突出。

現在我們來看看他們是如何實現的:

實現原理:

首先我們要明白 并不是應用本身處理對啟動圖標進行修改、圖標的動態修改的過程主要是在Launcher里面完成的.在應用安裝,更新,卸載的時候,都會有廣播發出,Launcher在LauncherApplication 中注冊廣播,在LauncherModel中處理接收到廣播的消息,重新加載更新應用信息(如:應用圖標、文字等)。但是原生的android系統是并不支持該特性的(及不能通過發送特定的系統廣播 達到動態修改啟動圖標的效果),但是在強大的第三方Android手機廠商(如:三星、小米)的系統源碼深度定制下、通過修改了Launcher源代碼,增加/注冊了新的廣播接收器用來接收應用發送來的未讀消息數廣播,接收到廣播后,系統將未讀消息的數目顯示事件交給Launcher去處理,調用相關方法去重繪應用的icon,最終達到動態更新應用圖標的效果。

示例代碼:

public class LauncherBadgeHelper { /**  * Set badge count  * 針對 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 {   sendToSamsumg(context, count);  } } /**  * 向小米手機發送未讀消息數廣播  *  * @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) {   LogController.e(e.toString());   // 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);  } } /**  * 向索尼手機發送未讀消息數廣播  * 據說:需添加權限:<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未讀顯示數  *  * @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; }}</uses-permission>

可以看出小米,三星,索尼處理方式都是通過發送廣播來實現的。

但是:小米MIUI6以后,改變了處理方式,棄用了發送廣播的方式,改為通過發送通知。

一、基本介紹

1、默認的情況

當app 向通知欄發送了一條通知 (通知不帶進度條并且用戶可以刪除的),那么桌面app icon角標就會顯示1.此時app顯示的角標數是和通知欄里app發送的通知數對應的,即向通知欄發送了多少通知就會顯示多少角標

二、實現代碼

第三方app需要用反射來調用,參考代碼:

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);Notification.Builder builder = new Notification.Builder(this).setContentTitle(“title”).setContentText(“text”).setSmallIcon(R.drawable.icon);Notification notification = builder.build();try {Field field = notification.getClass().getDeclaredField(“extraNotification”);Object extraNotification = field.get(notification);Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCount”, int.class);method.invoke(extraNotification, mCount);} catch (Exception e) {e.printStackTrace();}mNotificationManager.notify(0,notification);

自己在之前的代碼根據官方代碼總結新的方法如下:

/** * 向小米手機發送未讀消息數廣播miui6以后 * * @param count */ private static void sendToXiaoMi2(Context context, int count) {  NotificationManager mNotificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);  Notification.Builder builder = new Notification.Builder(MyApplication.getContext()).setContentTitle("title").setContentText("text").setSmallIcon(R.drawable.ico_haoyilogo);  Notification notification = null;  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {   notification = builder.build();  }  try {   Field field = notification.getClass().getDeclaredField("extraNotification");   Object extraNotification = field.get(notification);   Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);   method.invoke(extraNotification, count);   mNotificationManager.notify(10, notification);  } catch (Exception e) {   e.printStackTrace();   LogController.e(e.toString());   // 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);  } }

這樣既能兼容MIUI6之前的,還能實現MIUI6以后的。自己在開發的時候經過測試,發現MIUI內部對于相同的消息數字是不顯示的,由于我測試的時候用的是寫死的數字,導致走了很多彎路。還有,自己在查找資料的時候發現有許多朋友都遇到過這樣的問題,未讀消息數字只有在第一次安裝的時候才顯示,進入后再設置就沒有了,我估計都是因為數字相同造成的。

細想一下,MIUI這種做法也挺好的,消息數字和通知綁定,當來通知時觸發事件,從而桌面圖標數字動態改變。當我們清楚通知時,清空數字。自己也調研了iOS的做法,他們只是通過調用系統的一個方法將消息數字傳進去即可,做法類似于Android 通過發送廣播方式,和三星一樣。

那么,小米是如何做到累加的呢。

我們只需定義全局變量count,初始值設置為1,然后發送通知后,手動改變count值,count=count+1。

友情鏈接:

https://github.com/lixiangers/BadgeUtil

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 睢宁县| 阿合奇县| 昌图县| 增城市| 定陶县| 博兴县| 巍山| 新沂市| 绥江县| 建平县| 东海县| 利津县| 渑池县| 鸡泽县| 搜索| 眉山市| 军事| 天峻县| 济源市| 通化市| 申扎县| 寻乌县| 乐亭县| 华亭县| 鹤庆县| 苏尼特左旗| 蒙山县| 霍城县| 延津县| 阳信县| 扎兰屯市| 宁津县| 崇仁县| 广德县| 遂昌县| 凉山| 成武县| 宁陵县| 南城县| 富平县| 额尔古纳市|