android不使用推送,在桌面图标上显示消息数量
2017-11-21 15:07
1391 查看
类似于微信、qq等软件,在应用图标上显示未读消息数量,但是有一个缺陷,需要用户启动app,调用消息列表接口,获取消息数量之和才会去更新图标上的数字。 下载ShortcutBadger 库,androidstudio直接引用远程库“compile"me.leolin:ShortcutBadger:1.1.17@aar"”,在app启动页面和调用消息列表的节目,当接口返回数据后,直接调用ShortcutBadger.applyCount(context,badgeCount)方法,然后通过一个Service来更新桌面图标上的数字。这个是需要一个新建一个单独的Service , 代码如下:
package com.gyjdj.ui; import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import com.gyjdj.R; import me.leolin.shortcutbadger.ShortcutBadger; public class BadgeIntentService extends IntentService{ private int notificationId = 0; public BadgeIntentService() { super("BadgeIntentService"); } private NotificationManager mNotificationManager; @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } @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("") .setContentText("") .setSmallIcon(R.mipmap.ic_launcher); Notification notification = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { notification = builder.build(); } ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); mNotificationManager.notify(notificationId, notification); } } }
使用service需要在manifest 中注册
相关文章推荐
- Android中桌面图标显示通知消息的数量
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- iOS如何显示桌面应用图标上的推送消息数(大神勿进)
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示 Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android 桌面图标右上角显示未读消息数字
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android 桌面图标右上角显示未读消息数字
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- Android--使用Notification在通知栏显示消息
- 使用Google Cloud Messaging (GCM)服务实现Android消息推送
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
- 消息推送之百度云推送Android集成与使用方法
- 使用Androiddp推送消息的一些问题
- 使用Androidpn实现Android消息推送
- 使用Androidpn实现Android消息推送
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互