Android面试八股文,Android app图标显示未读消息数,一文轻松搞定

注意:示例源代码中MIUI系统的功能实现和官方介绍的方法不一样,工具类使用的是官方介绍的方法

这个工具类,参考的这篇博客的MIUI系统测试无效,我改了一下,小米测试可以。sony和三星未测试

工具类下载地址:http://download.csdn.net/detail/xx326664162/9484065

源代码


package cn.hdnc.BadgeUtil; import android.app.Notification; 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; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 应用启动图标未读消息数显示 工具类 (效果如: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(Notification notification,Context context, int count) { if (count <= 0) { count = 0; } else { count = Math.max(0, Math.min(count, 99)); } if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { sendToXiaoMi(notification, 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(Notification notification,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才行 Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, count); } 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(Notification notification,Context context) { setBadgeCount(notification, 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; } }

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

  • 45

  • 46

  • 47

  • 48

  • 49

  • 50

  • 51

  • 52

  • 53

  • 54

  • 55

  • 56

  • 57

  • 58

  • 59

  • 60

  • 61

  • 62

  • 63

  • 64

  • 65

  • 66

  • 67

  • 68

  • 69

  • 70

  • 71

  • 72

  • 73

  • 74

  • 75

  • 76

  • 77

  • 78

  • 79

  • 80

  • 81

  • 82

  • 83

  • 84

  • 85

  • 86

  • 87

  • 88

  • 89

  • 90

  • 91

  • 92

  • 93

  • 94

  • 95

  • 96

  • 97

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

总结

其实客户端开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

Android大厂面试真题全套解析

2017-2020字节跳动Android面试真题解析PDF
然而Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值