Android 在应用图标上设置未读消息数量

文章介绍了如何根据不同手机厂商的launcher(如华为、小米、摩托罗拉等)定制主题,并实现角标(badgenumber)的动态设置,涉及多个厂商的特定API调用方法。
摘要由CSDN通过智能技术生成
  • 华为手机launcher主题

  • Huawei mobile phone launcher theme

*/

private static final String HUWEI_LAUNCHERNAME = “com.huawei.android.launcher”;

/**

  • 小米手机launcher主题

  • MIUI phone launcher theme

*/

private static final String MIUI_LAUNCHERNAME = “com.miui.home”;

/**

  • 摩托罗拉和宏达手机launcher主题

  • Motorola and HTC mobile phone launcher theme

*/

private static final String MOTOANDHTC_LAUNCHERNAME = " com.android.launcher";

/**

  • 三星手机launcher主题

  • Samsung mobile phone launcher theme

*/

private static final String SAMSUNG_LAUNCHERNAME = “com.sec.android.app.launcher”;

/**

  • google 手机launcher主题

  • Google phone launcher theme

*/

private static final String GOOGLE_LAUNCHERNAME = “com.google.android.apps.nexuslauncher”;

private volatile static DesktopCornerUtil desktopCornerUtil;

/**

  • vivo手机厂商名字

  • Vivo mobile phone manufacturer name

*/

private static final String VIVO_MANUFACTURER_NAME = “vivo”;

/**

  • OPPO 手机厂商名字

  • OPPO mobile phone manufacturer name

*/

private static final String OPPO_MANUFACTURER_NANE = “OPPO”;

/**

  • 应用包名

  • Application package name

*/

private static String packageName = null;

/**

  • context 对象 这里建议传递Application的context 避免内存泄漏

  • It is recommended to pass the Application context to avoid memory leaks.

*/

static Context context = null;

/**

  • 主Activity的名字 为全路径 比如 com.***…**Activity

  • The name of the main Activity is the full path. For example, com.***…**Activity

*/

private static String mainActivityName = null;

/**

  • notification 对象 小米系统会默认设置 如果修改默认逻辑 必须获取notification对象

  • The millet system will default settings. If you modify the default logic,

  • you must get the notification object.

*/

private static Notification notification = null;

private DesktopCornerUtil() {

}

public static DesktopCornerUtil getInstance() {

if (desktopCornerUtil == null) {

synchronized (DesktopCornerUtil.class) {

if (desktopCornerUtil == null) {

desktopCornerUtil = new DesktopCornerUtil();

}

}

}

return desktopCornerUtil;

}

/**

  • 初始化 init 建议放在Application中执行

  • t is recommended to execute in Application

  • @param packageNameParameter @desc 应用包名

  • @param mainActivityNameParameter @desc MainActivity

  • @param contextParameter @desc BaseApplication context

*/

public static void init(String packageNameParameter, String mainActivityNameParameter, Context contextParameter) {

packageName = packageNameParameter;

context = contextParameter;

mainActivityName = mainActivityNameParameter;

}

/**

  • @param packageNameParameter @desc 应用包名

  • @param mainActivityNameParameter @desc MainActivity

  • @param notication 通知

  • @param contextParameter @desc BaseApplication context

*/

public static void init(String packageNameParameter, String mainActivityNameParameter, Notification notication, Context contextParameter) {

packageName = packageNameParameter;

context = contextParameter;

mainActivityName = mainActivityNameParameter;

}

/**

  • 获取launcher相关信息 根据launcher

  • 分别开发

  • miui: com.miui.home

  • huawei: com.huawei.android.launcher

  • moto and htc: com.android.launcher

  • samsung: com.sec.android.app.launcher

  • goole:com.google.android.apps.nexuslauncher

  • BaseApplication.getContext() is Application context

  • @return

*/

private static String getLauncherMessage() {

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_HOME);

if (context == null) {

return “”;

} else {

final ResolveInfo res = context

.getPackageManager().resolveActivity(intent, 0);

if (res.activityInfo == null) {

return “”;

}

if (context.getString(R.string.android).

equals(res.activityInfo.packageName)) {

return “”;

}

return res.activityInfo.packageName;

}

}

/**

  • 设置角标数字

  • Set the angle number

  • @param badgeNumber @desc 数量

*/

public static void setBadgeNumber(int badgeNumber) {

if (badgeNumber >= 0) {

switch (getLauncherMessage()) {

case HUWEI_LAUNCHERNAME:

setBadgeNumberHuawei(badgeNumber);

break;

case MIUI_LAUNCHERNAME:

setBadgeNumberMiui(badgeNumber);

break;

case MOTOANDHTC_LAUNCHERNAME:

setBadgeNumberhtc(badgeNumber);

break;

case SAMSUNG_LAUNCHERNAME:

setBadgeNumberSamsung(badgeNumber);

break;

case GOOGLE_LAUNCHERNAME:

setBadgeNumberGoogle(badgeNumber);

break;

default:

//再根据制造商去判断

String manufacturer = Build.MANUFACTURER;

switch (manufacturer) {

case VIVO_MANUFACTURER_NAME:

setBadgeNumberVivo(badgeNumber);

break;

case OPPO_MANUFACTURER_NANE:

setBadgeNumberOppo(badgeNumber);

break;

default:

break;

}

break;

}

} else {

// TODO: 2019/6/27 donothing

}

}

/**

  • Google手机设置角标 只支持8.0以上

  • Google phone settings corner Only supports 8.0 or above

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberGoogle(int badgeNumber) {

if (TextUtils.isEmpty(packageName)) {

return;

}

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {

return;

}

Intent intent = new Intent(“android.intent.action.BADGE_COUNT_UPDATE”);

intent.putExtra(“badge_count”, badgeNumber);

intent.putExtra(“badge_count_package_name”, packageName);

intent.putExtra(“badge_count_class_name”, mainActivityName);

context.sendBroadcast(intent);

}

/**

  • htc和摩托罗拉设置角标

  • Htc and Motorola set the corners

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberhtc(int badgeNumber) {

if (TextUtils.isEmpty(packageName)) {

return;

}

Intent intentNotification = new Intent(“com.htc.launcher.action.SET_NOTIFICATION”);

intentNotification.putExtra(“com.htc.launcher.extra.COMPONENT”, packageName);

intentNotification.putExtra(“com.htc.launcher.extra.COUNT”, badgeNumber);

context.sendBroadcast(intentNotification);

Intent intentShortcut = new Intent(“com.htc.launcher.action.UPDATE_SHORTCUT”);

intentShortcut.putExtra(“packagename”, packageName);

intentShortcut.putExtra(“count”, badgeNumber);

context.sendBroadcast(intentShortcut);

}

/**

  • 小米手机设置角标

  • MIUI mobile phone settings corner

  • 当APP处于前台时,数字会自动清空

  • When the app is in the foreground, the number will be automatically cleared.

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberMiui(int badgeNumber) {

try {

if (notification == null) {

return;

} else {

Field field = notification.getClass().getDeclaredField(“extraNotification”);

Object extraNotification = field.get(notification);

Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCoun”, int.class);

method.invoke(extraNotification, badgeNumber);

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

  • 华为手机设置角标

  • Huawei mobile phone settings corner

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberHuawei(int badgeNumber) {

if (TextUtils.isEmpty(packageName)) {

return;

}

Bundle extra = new Bundle();

extra.putString(“package”, packageName);

extra.putString(“class”, mainActivityName);

extra.putInt(“badgenumber”, badgeNumber);

context.getContentResolver().call(Uri.parse(“content://com.huawei.android.launcher.settings/badge/”),

“change_badge”, null, extra);

}

/**

  • 三星手机 设置角标

  • Samsung mobile phone set corner

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberSamsung(int badgeNumber) {

if (TextUtils.isEmpty(packageName)) {

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

image

image

image
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
i.parse(“content://com.huawei.android.launcher.settings/badge/”),

“change_badge”, null, extra);

}

/**

  • 三星手机 设置角标

  • Samsung mobile phone set corner

  • @param badgeNumber @desc 数量

*/

private static void setBadgeNumberSamsung(int badgeNumber) {

if (TextUtils.isEmpty(packageName)) {

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

[外链图片转存中…(img-r1N8dAYa-1714411220798)]

[外链图片转存中…(img-ef2Cs6R9-1714411220800)]

[外链图片转存中…(img-ORXvOv8j-1714411220801)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值