小米角标自定义设置

这篇博客介绍了如何在Android应用中自定义桌面角标数量,通过调用系统接口并结合第三方库ShortcutBadger来实现。当应用退到后台时,通过服务更新角标,确保角标与未读消息数量同步。同时提供了监听应用状态变化的方法,以便在进入后台时刷新角标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近接到一个 需求,需要把应用内未读消息的数量同步到桌面角标。官网文档是这么写的。

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

        虽说官网给出方法,但是调用之后一直没效果。结合之前小米推送,猜测有可能跟小米推送提示角标数的原理是一样的,就是角标数量跟通知有关。并且需要app在后台的时候,调用该方法刷新角标。

先推荐个类库:GitHub - leolin310148/ShortcutBadger: An Android library supports badge notification like iOS in Samsung, LG, Sony and HTC launchers.An Android library supports badge notification like iOS in Samsung, LG, Sony and HTC launchers. - GitHub - leolin310148/ShortcutBadger: An Android library supports badge notification like iOS in Samsung, LG, Sony and HTC launchers.https://github.com/leolin310148/ShortcutBadger

        然后接着往下看。

        想法就是监听app在退到后台的时候,调用刷新。

public class BadgeIntentService extends IntentService {

    private static final String NOTIFICATION_CHANNEL = "xiaomi";

    private int notificationId = 999;

    public BadgeIntentService() {
        super("BadgeIntentService");
    }

    private NotificationManager mNotificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            int badgeCount = intent.getIntExtra("badgeCount", 0);

            mNotificationManager.cancel(notificationId);
            //如果需要每次退到后台就显示角标数量 就注掉
            //notificationId++;

            Notification.Builder builder = new Notification.Builder(getApplicationContext())
                .setContentTitle("app")
                .setContentText("未读提醒")
                .setSmallIcon(R.drawable.logo);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                setupNotificationChannel();

                builder.setChannelId(NOTIFICATION_CHANNEL);
            }

            Notification notification = builder.build();
            ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount);
            mNotificationManager.notify(notificationId, notification);
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void setupNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, "ShortcutBadger Sample",
                NotificationManager.IMPORTANCE_DEFAULT);

        mNotificationManager.createNotificationChannel(channel);
    }
}

        记得去AndroidManifest中注册 !!       

        监听的方法放在Appication中去声明。

public class MyApp extends MultiDexApplication implements MultiDexApplication.ActivityLifecycleCallbacks {

    private int foregroundCount = 0; // 位于前台的 Activity 的数目
	
	
	@Override
    public void onActivityStarted(Activity activity) {
        if (foregroundCount <= 0) {
            //  这里处理从后台恢复到前台的逻辑
            try {
				//需要清除推送接收到的通知,否则计数会重复
                MiPushClient.clearNotification(getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        foregroundCount++;
    }
	
	@Override
    public void onActivityStopped(Activity activity) {
        foregroundCount--;

        if (foregroundCount <= 0) {
            //这里处理从前台进入到后台的逻辑
            try {
				startService(
					new Intent(getContext(), BadgeIntentService.class).putExtra("badgeCount", count)
                );
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

        就这些内容了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值