关于 小米手机 角标的问题

来源:互联网 发布:淘宝退换货要求 编辑:程序博客网 时间:2024/05/16 02:05

平常可能都会看到 比如 三星,小米, 系统有类似 ios上app消息通知效果的 右上角 消息角标

在网络上 也有相关方面的资料,但是也有一点是有问题的
就比如在 miui 6 上
这个查了相关资料后,其实是这样的

小米应用开发者文档

默认的情况
当app 向通知栏发送了一条通知 (通知不带进度条并且用户可以删除的),那么桌面app icon角标就会显示1.此时app显示的角标数是和通知栏里app发送的通知数对应的,即向通知栏发送了多少通知就会显示多少角标。
通知可以定义角标数
例如 有5封未读邮件,通知栏里只会显示一条通知,但是想让角标显示5. 可以在发通知时加个标示。

第三方app需要用反射来调用,参考代码:NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);Notification.Builder builder = new Notification.Builder(this).setContentTitle(“title”).setContentText(“text”).setSmallIcon(R.drawable.icon);Notification notification = builder.build();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();}mNotificationManager.notify(0,notification);

相关链接
http://dev.xiaomi.com/doc/p=3904/index.html

不过说实话,这个功能真的挺鸡肋了,

确实 很多 apk是按照 第一种方式 默认的,想想也对,有时候完全没有必要因为小米系统的变化,一直去修改这方面角标显示的代码,默认应该是最好的了。

第2种, 我当时用 小米 note测试了,
在第一次转上时,执行代码,确实有了角标,但是当我点进去,或者清除状态栏角标消失后,我清除后台,
我重新进入apk,虽然代码是调用了,但是并没有角标生成,这个就很奇怪了,具体原因也真的不知道,

为什么第一次就好好的,重新进入切调用了代码却没有角标的生成,如果有知道的人,看到了请给我评论里说下
这里写图片描述

  1. 获取判断 miui 版本

String miui = “ro.miui.ui.version.name”;
参考Android源码:
https://code.google.com/p/cyanogen-updater/source/browse/trunk/src/cmupdaterapp/utils/SysUtils.java#19

在Android shell模式下输入 getprop 就能获取系统属性值
如果Rom是miUI那么就会有以下字段.

public static String getSystemProperty(String propName){        String line;        BufferedReader input = null;    try{        Process p = Runtime.getRuntime().exec("getprop " + propName);        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);        line = input.readLine();        input.close();      }catch (Exception ex){        Log.e("miui", "Unable to read sysprop " + propName, ex);        return null;      }finally{        if(input != null){                try{                        input.close();                }catch (Exception e){                        Log.e("miui", "Exception while closing InputStream", e);                }        }        }       return line;       }

测试后获取到的 V5 V6

参考:http://blog.csdn.net/devilkin64/article/details/19415717

再就是 网络上都有的一种 miui 6 前的 角标实现的方式
测试过还是可以用的,但是他判断 miui6 的代码貌似是不对的
在 5 上测试过是可以成功的

public class Miutil{    /**     * Set badge count<br/>     * 针对 Samsung / xiaomi / sony 手机有效     * @param context The context of the application package.     * @param count Badge count to be set     */    public static void setBadgeCount(Context context, int count) {        if (count <= 0) {            count = 0;        } else {            count = Math.max(0, Math.min(count, 99));        }        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {            System.out.println("1111111111111111111111");            sendToXiaoMi(context, count);        } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {            sendToSony(context, count);        } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {            sendToSamsumg(context, count);        } else {            Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();        }    }    /**     * 向小米手机发送未读消息数广播     * @param count     */    private static void sendToXiaoMi(Context context, int count) {        try {            ////////////这段代码是有问题的///////////////////////////////////////////            Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");            Object miuiNotification = miuiNotificationClass.newInstance();            Field field = miuiNotification.getClass().getDeclaredField("messageCount");            field.setAccessible(true);            field.set(miuiNotification, String.valueOf(count == 0 ? "" : count));  // 设置信息数-->这种发送必须是miui 6才行        } catch (Exception e) {            e.printStackTrace();            // miui 6之前的版本            Intent localIntent = new Intent(                    "android.intent.action.APPLICATION_MESSAGE_UPDATE");            System.out.println("===luancher ppp="+  context.getPackageName() + "/" + getLauncherClassName(context));            localIntent.putExtra(                    "android.intent.extra.update_application_component_name",                    context.getPackageName() + "/" + getLauncherClassName(context));            localIntent.putExtra(                    "android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count));            context.sendBroadcast(localIntent);            System.out.println("5555555555555555");        }    }    /**     * 向索尼手机发送未读消息数广播<br/>     * 据说:需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未验证]     * @param count     */    private static void sendToSony(Context context, int count){        String launcherClassName = getLauncherClassName(context);        if (launcherClassName == null) {            return;        }        boolean isShow = true;        if (count == 0) {          isShow = false;        }        Intent localIntent = new Intent();        localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);//是否显示        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",launcherClassName );//启动页        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));//数字        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名        context.sendBroadcast(localIntent);    }    /**     * 向三星手机发送未读消息数广播     * @param count     */    private static void sendToSamsumg(Context context, int count){        String launcherClassName = getLauncherClassName(context);        if (launcherClassName == null) {            return;        }        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);    }    /**     * 重置、清除Badge未读显示数<br/>     * @param context     */    public static void resetBadgeCount(Context context) {        setBadgeCount(context, 0);    }    /**     * Retrieve launcher activity name of the application from the context     *     * @param context The context of the application package.     * @return launcher activity name of this application. From the     *         "android:name" attribute.     */    private static String getLauncherClassName(Context context) {        PackageManager packageManager = context.getPackageManager();        Intent intent = new Intent(Intent.ACTION_MAIN);        // To limit the components this Intent will resolve to, by setting an        // explicit package name.        intent.setPackage(context.getPackageName());        intent.addCategory(Intent.CATEGORY_LAUNCHER);        // All Application must have 1 Activity at least.        // Launcher activity must be found!        ResolveInfo info = packageManager                .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);        // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER        // if there is no Activity which has filtered by CATEGORY_DEFAULT        if (info == null) {            info = packageManager.resolveActivity(intent, 0);        }        return info.activityInfo.name;    }}

调用的代码

Miutil.setBadgeCount(getApplicationContext(), 10);

所以综上看,还是不要去管这些, 他自己变系统版本就自己变好了,看了很多app 也都是这样做的。因为这个标准并不是所有人都会去遵守的,一更新miui的版本可能相应的代码需要修改,就需要出个新的版本更新迭代。

1 0