Android 角标适配

小米手机

  1. 默认逻辑

当应用向通知栏发送了一条通知 (除了进度条样式和常驻通知外),应用图标的右上角就会显示「1」。值得一提,角标的数字代表应用的通知数,即应用发送了「x」条通知,角标就会显示为「x」。
2. 开发者如何自定义角标数

如果开发者不满意默认逻辑,想要自定义角标的数字,可以通过调用接口告知系统,参考代码如下:

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();
}

官网地址

华为
1、声明权限

< uses - permission android: name = "android.permission.INTERNET" / >
    <
    uses - permission android: name = "com.huawei.android.launcher.permission.CHANGE_BADGE " / >

2、在需要进行角标显示地方,采用如下方法传递数据给华为桌面应用。

Bundle extra = new Bundle();
extra.putString("package", "xxxxxx");
extra.putString("class", "yyyyyyy");
extra.putInt("badgenumber", i);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);

关键参数说明:

package:应用包名
class:桌面图标对应的应用入口Activity类
badgenumber:角标数字

示例:

boolean mIsSupportedBade = true;
if (mIsSupportedBade) {
    setBadgeNum(num);
}
/** set badge number*/
public void setBadgeNum(int num) {
    try {
        Bundle bunlde = new Bundle();
        bunlde.putString("package", "com.test.badge"); // com.test.badge is your package name
        bunlde.putString("class", "com.test. badge.MainActivity"); // com.test. badge.MainActivity is your apk main activity
        bunlde.putInt("badgenumber", num);
        this.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bunlde);
    } catch (Exception e) {
        mIsSupportedBade = false;
    }
}

特殊情形考虑:

对于第三应用打开和退出时,是否还继续显示角标取决应用传递的值(badgenumber为0时,不显示角标;badgenumber大于0时,显示角标)。
当第三方应用package和class发生变化时,需传递该变化后的信息。

官网地址

OPPO
若要使用角标功能,必须提交申请,审核通过了才能开放,官方给的具体审核标准如下:

申请角标接入规则(应用必须适配OPPO手机,保证角标功能测试通过)

a) 系统应用;

b) 国内外各区域用户量排名Top5的三方即时通讯类应用,且只允许显示即时通信消息类通知(如QQ、微信、facebook、line);

c) OPPO公司内部费商业化及运营性质的办公类型即时通信应用(如Teamtalk);

d) 国内外邮件类应用(各区域各属于用户量第一梯队的应用)

VIVO
申请都申请不了

三星

    private static boolean setSamsungBadge(int count, Context context) {
        try {
            String launcherClassName = getLauncherClassName(context);
            if (TextUtils.isEmpty(launcherClassName)) {
                return false;
            }
            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);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

联想ZUK

  1. 声明权限
<uses-permission android:name="android.permission.READ_APP_BADGE"/> 
  1. 定义ContentProvider的uri
private final Uri CONTENT_URI = Uri.parse("content://" + "com.android.badge" + "/" + "badge"); 

3.写入对应的角标需要显示的数值,示例代码如下:

private void testreddot(Context context, int counts){       

Bundle extra = new Bundle();

ArrayList<String> ids = new ArrayList<String>();

// 以列表形式传递快捷方式id,可以添加多个快捷方式id

ids.add("custom_id_1");

ids.add("custom_id_2");

………………..

extra.putStringArrayList("app_shortcut_custom_id", ids);

extra.putInt("app_badge_count", counts);

Bundle b = null;

b = context.getContentResolver().call(CONTENT_URI,"setAppBadgeCount", null, extra);

boolean result = false;

if (b != null) {

result = true;

}else {

result = false;  

}  

return;

} 

说明:

示例中ids这个参数可以为空或者“null”表示对主图标进行角标标记;custom_id_1等id值,是应用自定义的对应快捷方式的id,如果应用有快捷方式在桌面创建需要将此id传给桌面在后面将有说明。

这个例子中如果有多个id,那么表示多个id的角标值是一样的为counts。如果每个id有不同的值,需要分别循环调用并设置值,也就是一个id和对应counts值调用一次此接口。如果ids为null将更新主图标。

4.对需要在桌面创建快捷方式并有角标的应用需要在创建快捷方式的一个示例如下:

Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));

// 快捷方式的名称


shortcut.putExtra("duplicate", false); // 不允许重复创建

Intent shortcutIntent = new Intent();

shortcutIntent.setClassName("com.example","com.example.MainActivity");

// com.example是包名,com.example.MainActivity是启动页类名

shortcutIntent.setPackage(packageName);

shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); // 快捷方式的图标

shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

shortcut.putExtra("app_shortcut_custom_id","example_custom_id");

// 以extra参数的形式传递shortcutCustomId

sendBroadcast(shortcut);

//系统接收到该广播,需将shortcutCustomId与图标的对应关系存储到数据库      

说明:setClassName不是必须的。

接口使用示例

  1. 仅仅对主图标进行角标设置

    参数:

    context 应用上下文;

    counts 主图标的角标数量。

  2. 快捷方式角标设置

 参数:

 context 应用上下文;

 counts 快捷方式的角标数量;

 custom_id_1,custom_id_2 应用自己定义的值,这个值和创建快捷方式发送的广播中intent的值一致。

注意: 应用使用SDK后需要同时打开相关设置项才能生效。

设置 > 通知和状态栏 > 应用角标管理 在应用列表里打开您的应用的角标使用权限。

适用版本: ZUI 1.1 以上

官网地址

HTC

    private static boolean setHTCBadge(int count, Context context) {
        try {
            ComponentName launcherComponentName = getLauncherComponentName(context);
            if (launcherComponentName == null) {
                return false;
            }
 
            Intent intent1 = new Intent("com.htc.launcher.action.SET_NOTIFICATION");
            intent1.putExtra("com.htc.launcher.extra.COMPONENT", launcherComponentName
                    .flattenToShortString());
            intent1.putExtra("com.htc.launcher.extra.COUNT", count);
            context.sendBroadcast(intent1);
 
            Intent intent2 = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");
            intent2.putExtra("packagename", launcherComponentName.getPackageName());
            intent2.putExtra("count", count);
            context.sendBroadcast(intent2);
 
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

诺基亚

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();
}

索尼

  /**
     * Insert a badge associated with the specified package and activity names
     * asynchronously. The package and activity names must correspond to an
     * activity that holds an intent filter with action
     * "android.intent.action.MAIN" and category
     * "android.intent.category.LAUNCHER" in the manifest. Also, it is not
     * allowed to publish badges on behalf of another client, so the package and
     * activity names must belong to the process from which the insert is made.
     * To be able to insert badges, the app must have the PROVIDER_INSERT_BADGE
     * permission in the manifest file. In case these conditions are not
     * fulfilled, or any content values are missing, there will be an unhandled
     * exception on the background thread.
     *
     * @param badgeCount the badge count
     * @param packageName the package name
     * @param activityName the activity name
     */
    private void insertBadgeAsync(int badgeCount, String packageName, String activityName) {
        final ContentValues contentValues = new ContentValues();
        contentValues.put(BadgeProviderContract.Columns.BADGE_COUNT, badgeCount);
        contentValues.put(BadgeProviderContract.Columns.PACKAGE_NAME, packageName);
        contentValues.put(BadgeProviderContract.Columns.ACTIVITY_NAME, activityName);

        // The badge must be inserted on a background thread
        mQueryHandler.startInsert(0, null, BadgeProviderContract.CONTENT_URI, contentValues);
    }

GitHub地址

原生Android
接近原生Android手机:Pixel 、Nexus 、OnePlus;
前两个在国内很少有用的;OnePlus 用的也是稍稍的一部分

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();
}

官网地址

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夢鑰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值