怎么在Android應(yīng)用中添加一個(gè)未讀消息提示功能?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元鼓樓做網(wǎng)站,已為上家服務(wù),為鼓樓各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
示例代碼:
public class LauncherBadgeHelper { /** * Set badge count * 針對 Samsung / xiaomi / sony 手機(jī)有效 * * @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); } } /** * 向小米手機(jī)發(fā)送未讀消息數(shù)廣播 * * @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)); // 設(shè)置信息數(shù)-->這種發(fā)送必須是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); } } /** * 向索尼手機(jī)發(fā)送未讀消息數(shù)廣播 * 據(jù)說:需添加權(quán)限:[未驗(yàn)證] * * @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));//數(shù)字 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名 context.sendBroadcast(localIntent); } /** * 向三星手機(jī)發(fā)送未讀消息數(shù)廣播 * * @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未讀顯示數(shù) * * @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; } }
可以看出小米,三星,索尼處理方式都是通過發(fā)送廣播來實(shí)現(xiàn)的。
但是:小米MIUI6以后,改變了處理方式,棄用了發(fā)送廣播的方式,改為通過發(fā)送通知。
一、基本介紹
1、默認(rèn)的情況
當(dāng)app 向通知欄發(fā)送了一條通知 (通知不帶進(jìn)度條并且用戶可以刪除的),那么桌面app icon角標(biāo)就會顯示1.此時(shí)app顯示的角標(biāo)數(shù)是和通知欄里app發(fā)送的通知數(shù)對應(yīng)的,即向通知欄發(fā)送了多少通知就會顯示多少角標(biāo)
二、實(shí)現(xiàn)代碼
第三方app需要用反射來調(diào)用,參考代碼:
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);
自己在之前的代碼根據(jù)官方代碼總結(jié)新的方法如下:
/** * 向小米手機(jī)發(fā)送未讀消息數(shù)廣播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之前的,還能實(shí)現(xiàn)MIUI6以后的。自己在開發(fā)的時(shí)候經(jīng)過測試,發(fā)現(xiàn)MIUI內(nèi)部對于相同的消息數(shù)字是不顯示的,由于我測試的時(shí)候用的是寫死的數(shù)字,導(dǎo)致走了很多彎路。還有,自己在查找資料的時(shí)候發(fā)現(xiàn)有許多朋友都遇到過這樣的問題,未讀消息數(shù)字只有在第一次安裝的時(shí)候才顯示,進(jìn)入后再設(shè)置就沒有了,我估計(jì)都是因?yàn)閿?shù)字相同造成的。
細(xì)想一下,MIUI這種做法也挺好的,消息數(shù)字和通知綁定,當(dāng)來通知時(shí)觸發(fā)事件,從而桌面圖標(biāo)數(shù)字動態(tài)改變。當(dāng)我們清楚通知時(shí),清空數(shù)字。自己也調(diào)研了iOS的做法,他們只是通過調(diào)用系統(tǒng)的一個(gè)方法將消息數(shù)字傳進(jìn)去即可,做法類似于Android 通過發(fā)送廣播方式,和三星一樣。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。