Android设置角标提示

转载自:https://blog.csdn.net/dbs1215/article/details/53054073

**1、三星**

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

**2、华为**

String launcherClassName = getLauncherClassName(context);
if (launcherClassName != null) {
Bundle extra = new Bundle();
extra.putString("package", ct.getPackageName());
extra.putString("class", launcherClassName);
extra.putInt("badgenumber", count);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
}

**3、MIUI**

MIUI6以下
Intent intent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
intent.putExtra("android.intent.action.APPLICATION_MESSAGE_UPDATE","your app packageName/.LAUNCHER ActivityName");
intent.putExtra(EXTRA_UPDATE_APPLICATION_MESSAGE_TEXT, unreadCount);
context.sendBroadcast(intent);

MIUI6以上
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("小米角标")
                .setContentText("miui桌面角标消息");

        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
        Notification notification = builder.build();
        try {
            Object miuiNotification = Class.forName("android.app.MiuiNotification").newInstance();
            Field field = miuiNotification.getClass().getDeclaredField("messageCount");
            field.setAccessible(true);
            field.set(miuiNotification, Integer.valueOf(count));
            notification.getClass().getField("extraNotification").set(notification, miuiNotification);
            managerCompat.notify(0, notification);

        }  catch (Exception e) {
            e.printStackTrace();
        }

**4、Sony**

Intent intent = new Intent();
String launcherclassname = getLauncherClassName(context);
if (launcherclassname != null) {
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", unReadCount > 0);
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherclassname);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", unReadCount < 1 ? "" : unReadCount);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
context.sendBroadcast(intent);
}

**5、VIVO**

String launcherclassname = getLauncherClassName(context);
if (launcherclassname != null) {
Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
//Constants.KEY_PKG_NAME = "packageName"
//Constants.KEY_CLASS_NAME = "className"
intent.putExtra(Constants.KEY_PKG_NAME, context.getPackageName());
intent.putExtra(Constants.KEY_CLASS_NAME, launcherclassname);
intent.putExtra("notificationNum", unReadCount);
context.sendBroadcast(intent);
}

**6、OPPO**

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);
} catch (Throwable th) {}

Method: getLauncherClassName

public static String getLauncherClassName(Context context) {
       if(context == null){
           return null;
       }    
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        try {
            for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
                if (resolveInfo.activityInfo.applicationInfo.packageName.equalsIgnoreCase(context.getPackageName())) {
                    return resolveInfo.activityInfo.name;
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

**除了上述的代码,还需要在清单文件声明对应的权限**

<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>

/***获取手机信息**/
    private String getHandSetInfo() {
          String handSetInfo = "手机型号:" + android.os.Build.MODEL
                + "\n系统版本:" + android.os.Build.VERSION.RELEASE
                + "\n产品型号:" + android.os.Build.PRODUCT
                + "\n版本显示:" + android.os.Build.DISPLAY
                + "\n系统定制商:" + android.os.Build.BRAND + "\n设备参数:" + android.os.Build.DEVICE
                + "\n开发代号:" + android.os.Build.VERSION.CODENAME + "\nSDK版本号:" + android.os.Build.VERSION.SDK_INT + "\nCPU类型:" + android.os.Build.CPU_ABI + "\n硬件类型:" + android.os.Build.HARDWARE + "\n主机:" + android.os.Build.HOST + "\n生产ID:" + android.os.Build.ID + "\nROM制造商:" + android.os.Build.MANUFACTURER // 这行返回的是rom定制商的名称
                 ;
         return handSetInfo;
    }

猜你喜欢

转载自blog.csdn.net/weixin_40391500/article/details/80061582