Android Icon数字角标(BadgeNumber)的实现方式

http://blog.csdn.net/janice0529/article/details/44344169

Android系统 小米,三星,索尼手机发送桌面快键提醒数字图标,在Android系统中,众所周知不支持BadgeNumber,虽然第三方控件BadgeView可以实现应用内的数字提醒,但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。但幸运的是,某些ROM厂商提供了私有的API,但也带来了难度,API的不同意意味着代码量的增加和兼容性问题更加突出。

我们现在来实现桌面logo或者说icon右上角的图标,先来看2张图,第一张来自互联网,第二张来自个人实践!(由于实验条件有限,只能测试小米的(⊙o⊙)…,有兴趣的同学测试一下其他的吧)

    

好了,上代码

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

注意lancherActivityClassName 必须被配置为 启动页   android.intent.category.LAUNCHER

 <activity
            android:name="com.sample.activites.Welcome"
            android:configChanges="locale|keyboard|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>
时间: 2024-08-09 02:19:01

Android Icon数字角标(BadgeNumber)的实现方式的相关文章

Android 为应用添加数字角标

今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道android系统默认是不支持角标的.但是有时候你又可以在很多系统上看到角标,这些系统包括 小米手机的miui 三星手机的TouchWiz  索尼手机; 这些手机的系统应用都可以显示数字角标.这是应为这些系统进行了定制,使用的是自己的launcher. 所以在这些系统上的实现思路就是使用这些手机的私有a

iOS 实现角标 新消息提示红点 数字角标

镔哥今天写写实习新消息提示的小圆圈数字角标 直接上代码吧. 1:直接复杂uibarButton类 // //  UIBarButtonItem+Badge.h //  therichest // //  Created by 淘股 on 2015-05-05. //  Copyright (c) 2015 taogu Inc. All rights reserved. // #import <UIKit/UIKit.h> @interface UIBarButtonItem (Badge) @

HTML5 mui框架(声明方式)(折叠面板)(数字角标)(按钮)

声明方式(HBuilder会自动生成) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <

MUI - actionsheet(操作表)、badge(数字角标)

原文地址:http://www.hcoder.net/tutorials/info_83.html 1.操作表 actionsheet一般从底部弹出,显示一系列可供用户选择的操作按钮: actionsheet是从popover控件基础上演变而来,实际上就是一个固定从底部弹出的popover,故DOM结构和popove类似,只是需要在含.mui-popover类的节点上增加.mui-popover-bottom..mui-popover-action类: <div id="sheet1&qu

ios uibutton加数字角标

http://www.jianshu.com/p/0c7fae1cadac 第一种:https://github.com/mikeMTOL/UIBarButtonItem-Badge第二种:https://github.com/cwRichardKim/RKNotificationHub JSBadgeView  可以设置在任何view的很多位置,很好用 按钮右上角加个数字红点JSBadgeView *badgeView = [[JSBadgeView alloc]initWithParentV

IOS开发 应用程序图标数字角标

其实实现这个功能很简单,只要调用UIApplication即可. 用法用例:[UIApplication sharedApplication].applicationIconBadgeNumber=33; 当用户打开应用程或者退出应用程序之前把这个值归0就OK了. [UIApplication sharedApplication].applicationIconBadgeNumber=0;

Android Badge给应用添加角标

应用角标是iOS的一个特色,原生Android并不支持.或许是因为当时iOS的通知栏比较鸡肋(当然现在已经改进了很多),而Android的通知栏功能强大?所以才出现了一方依赖于数字角标,一方坚持强大的通知栏,在日常使用中这两种交互方式都各有特色,没什么违和感.但是啊,总有人想搞些大新闻: 当收到推送而应用没有未读角标时:安卓---什么烂手机!安卓就是不行!苹果---我靠,这应用竟然连角标都不支持!删掉. 所以啊,众多手机厂商的定制系统或者Launcher都效仿iOS自定义了该功能.如果嫌适配麻烦

Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

参考: http://dev.xiaomi.com/doc/p=3904/index.html http://my.oschina.net/ososchina/blog/352286?p=1#comments https://github.com/leolin310148/ShortcutBadger http://www.voidcn.com/blog/kongbaidepao/article/p-62251.html http://www.eoeandroid.com/thread-5572

IOS应用桌面icon上不显示角标问题修复

今天在做消息通知,要在桌面图标的icon上显示消息数,例如 ,在程序里也加上了代码:[UIApplication sharedApplication].applicationIconBadgeNumber = 25; 但是怎么搞就是不显示,最后查资料发现在ios8系统里必须先得让用户授权才能显示,可以在代码里加上如下代码 if (IS_IOS8) { UIUserNotificationType myType = UIRemoteNotificationTypeBadge | UIRemoteN