Android Icon数字角标(BadgeNumber)的实现方式
2016-07-24 17:32
661 查看
Android系统 小米,三星,索尼手机发送桌面快键提醒数字图标,在Android系统中,众所周知不支持BadgeNumber,虽然第三方控件BadgeView可以实现应用内的数字提醒,但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。但幸运的是,某些ROM厂商提供了私有的API,但也带来了难度,API的不同意意味着代码量的增加和兼容性问题更加突出。
我们现在来实现桌面logo或者说icon右上角的图标,先来看2张图,第一张来自互联网,第二张来自个人实践!(由于实验条件有限,只能测试小米的(⊙o⊙)…,有兴趣的同学测试一下其他的吧)
好了,上代码
注意lancherActivityClassName 必须被配置为 启动页 android.intent.category.LAUNCHER
我们现在来实现桌面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>
相关文章推荐
- android 杀不死的服务 ,主要是即时通讯,通知及时传送
- android 桌面快捷键未读的消息数目,未接来电,短信提醒那个右上角的数字
- Android 中的 Service 全面总结
- Android开发之鬼迷心窍(二)
- Android初级教程通过简要分析“土司”源码,来自实现定义土司理论探讨
- Android初级教程通过简要分析“土司”源码,来自实现定义土司理论探讨
- 利用JitPack发布Android开源库
- android----菜单
- 浅析Java匿名内部类在Android事件响应中的应用
- Android Device Administration 设备管理器——实现一键锁屏
- Failed to load the LayoutLib: com/android/layoutlib/bridge/Bridge : Unsupported major.minor version
- Android Device Administration 设备管理器——实现一键锁屏
- android launchmode(四种启动模式)应用场景及实例
- Andoid 总线设计 (一)
- Android AIDL
- 安卓事件传递机制
- Android利用Gson解析嵌套多层的Json的简单介绍
- LocationManager、Criteria、Geocoder的用法
- android studio 常用快捷键
- android一个LinearLayout中具有几个控件,想要使那几个控件在LinearLayout被点击时,有颜色变化