当应用向通知栏发送了一条通知 (除了进度条样式和常驻通知外),应用图标的右上角就会显示「1」。值得一提,角标的数字代表应用的通知数,即应用发送了「x」条通知,角标就会显示为「x」。
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()
}
huawei:
BadgeNotification add_num 否 integer 应用角标累加数字非应用角标实际显示数字 例如,某应用当前有N条未读消息,若add_num设置为3,则每发一次消息,应用角标显示的数字累加3,为N+3
/** 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
}
}
oppo:
老版本方法
private static boolean setOPPOBadge(int count, Context context) {
try {
Bundle extras = new Bundle()
extras.putInt("app_badge_count", count)
context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
"setAppBadgeCount", String.valueOf(count), extras)
return true
} catch (Exception e) {
e.printStackTrace()
return false
}
}
private static boolean setOPPOBadge2(int count, Context context) {
try {
Intent intent = new Intent("com.oppo.unsettledevent")
intent.putExtra("packageName", context.getPackageName())
intent.putExtra("number", count)
intent.putExtra("upgradeNumber", count)
PackageManager packageManager = context.getPackageManager()
List<ResolveInfo>receivers = packageManager.queryBroadcastReceivers(intent, 0)
if (receivers != null &&receivers.size() >0) {
context.sendBroadcast(intent)
} else {
Bundle extras = new Bundle()
extras.putInt("app_badge_count", count)
context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
"setAppBadgeCount", null, extras)
}
return true
} catch (Exception e) {
e.printStackTrace()
return false
}
}
新版本:
新款的OPPO角标功能仅支持内置应用、微信和QQ显示角标,若要使用角标功能,必须提交申请,审核通过了才能开放,官方给的具体审核标准如下:
申请角标接入规则(应用必须适配OPPO手机,保证角标功能测试通过)
a) 系统应用;
b) 国内外各区域用户量排名Top5的三方即时通讯类应用,且只允许显示即时通信消息类通知(如QQ、微信、facebook、line);
c) OPPO公司内部费商业化及运营性质的办公类型即时通信应用(如Teamtalk);
d) 国内外邮件类应用(各区域各属于用户量第一梯队的应用)。
vivo:
桌面图标角标”默认关闭,需要用户手动开启。
开启路径:“设置”-“通知与状态栏”-“应用通知管理”-应用名称-“桌面图标角标”。
未成功接入“桌面图标角标”的应用,无“桌面图标角标”选项。
备注:视OS版本差异,“桌面图标角标”名称可能为“应用图标标记”或“桌面角标”。
a. 添加权限:
<uses-permission android:name="com.vivo.notification.permission.BADGE_ICON" />
b. 应用在需要显示桌面角标的场景,通过广播将信息发送给vivoLauncher:
广播参数:
action:launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM
packageName:应用包名
className:主类名
notificationNum:未读消息数目
简单示例:
Intent intent = new Intent()
int missedCalls = 10
intent.setAction("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM")
intent.putExtra("packageName", "com.android.xxxx")
intent.putExtra("className", "com.android.xxxx.Mainxxxx")
intent.putExtra("notificationNum", missedCalls)
intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
sendBroadcast(intent)
注意:
在8.0上,还需要给Intent加上下面的flag
Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
sansung:
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
}
}
应用角标是iOS的一个特色,原生Android并不支持。在项目开发的过程中,项目需求在手机桌面图标上显示新消息条数的角标,桌面角标的功能在原生android系统中是没有提供此类API的,只有第三方深度定制过的android系统才有此类功能,如:三星、小米、魅族、华为等,但问题又来了,每家定制使用的方法又只不相同,我们一般使用第三方开源的项目来实现。
这里提供两个Github上的项目:
https://github.com/leolin310148/ShortcutBadger
https://github.com/xuyisheng/ShortcutHelper
这里以ShortcutBadger为例进行解读
添加依赖支持
compile "me.leolin:ShortcutBadger:1.1.13@aar"
添加消息角标
ShortcutBadger.applyCount(context, number)
移除消息
boolean isRemoveSuccess = ShortcutBadger.removeCount(this)
权限问题
有些手机上无法生效,无意发现申请了 之后就解决了
效果
如图是lg上的显示效果
参考:
Android上的Badge,快速实现给应用添加角标
ShortcutBadgerSample
public class MainActivity extends Activity {//必须使用,Activity启动页
private final static String lancherActivityClassName = Welcome.class.getName()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.common_listview_layout)
}
@Override
protected void onResume() {
super.onResume()
sendBadgeNumber()
}
private void sendBadgeNumber() {
String number = "35"
if (TextUtils.isEmpty(number)) {
number = "0"
} else {
int numInt = Integer.valueOf(number)
number = String.valueOf(Math.max(0, Math.min(numInt, 99)))
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
sendToXiaoMi(number)
} else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
sendToSony(number)
} else if (Build.MANUFACTURER.toLowerCase().contains("sony")) {
sendToSamsumg(number)
} else {
Toast.makeText(this, "Not Support", Toast.LENGTH_LONG).show()
}
}
private void sendToXiaoMi(String number) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
Notification notification = null
boolean isMiUIV6 = true
try {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
builder.setContentTitle("您有"+number+"未读消息")
builder.setTicker("您有"+number+"未读消息")
builder.setAutoCancel(true)
builder.setSmallIcon(R.drawable.common_icon_lamp_light_red)
builder.setDefaults(Notification.DEFAULT_LIGHTS)
notification = builder.build()
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification")
Object miuiNotification = miuiNotificationClass.newInstance()
Field field = miuiNotification.getClass().getDeclaredField("messageCount")
field.setAccessible(true)
field.set(miuiNotification, number)// 设置信息数
field = notification.getClass().getField("extraNotification")
field.setAccessible(true)
field.set(notification, miuiNotification)
Toast.makeText(this, "Xiaomi=>isSendOk=>1", Toast.LENGTH_LONG).show()
}catch (Exception e) {
e.printStackTrace()
//miui 6之前的版本
isMiUIV6 = false
Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE")
localIntent.putExtra("android.intent.extra.update_application_component_name",getPackageName() + "/"+ lancherActivityClassName )
localIntent.putExtra("android.intent.extra.update_application_message_text",number)
sendBroadcast(localIntent)
}
finally
{
if(notification!=null &&isMiUIV6 )
{
//miui6以上版本需要使用通知发送
nm.notify(101010, notification)
}
}
}
private void sendToSony(String number) {
boolean isShow = true
if ("0".equals(number)) {
isShow = false
}
Intent localIntent = new Intent()
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow)//是否显示
localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE")
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",lancherActivityClassName )//启动页
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number)//数字
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",getPackageName())//包名
sendBroadcast(localIntent)
Toast.makeText(this, "Sony," + "isSendOk", Toast.LENGTH_LONG).show()
}
private void sendToSamsumg(String number)
{
Intent localIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE")
localIntent.putExtra("badge_count", number)//数字
localIntent.putExtra("badge_count_package_name", getPackageName())//包名
localIntent.putExtra("badge_count_class_name",lancherActivityClassName )//启动页
sendBroadcast(localIntent)
Toast.makeText(this, "Samsumg," + "isSendOk", Toast.LENGTH_LONG).show()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)