您的位置:首页 > 移动开发 > Android开发

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 中注册
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐