首页 文章

如何在app launcher图标中显示通知计数[重复]

提问于
浏览
172

这个问题在这里已有答案:

三星galaxy note 2 android版4.1.2

我知道之前曾问过这个问题而且回复是不可能的

如何在android上的应用程序启动器图标上显示气球计数器

然而昨天我更新了facebook应用程序,它开始显示未读消息私人消息的计数器 . 为什么Facebook应用程序可以,我不能为我的应用程序这样做?

facebook icon

enter image description here

三星galaxy note 2 android版4.1.2

5 回答

  • 113

    Android(没有自定义启动器和触摸界面的"vanilla" android) does not 允许更改应用程序图标,因为一旦编译程序,它就会被密封在 .apk 中 . 无法使用标准API以编程方式将其更改为'drawable' . 您可以使用小部件而不是图标来实现目标 . 小部件是可自定义的 . 请阅读:http://www.cnet.com/8301-19736_1-10278814-251.htmlhttp://developer.android.com/guide/topics/appwidgets/index.html . 另见:https://github.com/jgilfelt/android-viewbadger . 它可以帮助你 .

    至于徽章编号 . 正如我之前所说的那样 - 没有标准的方法可以做到这一点 . 但我们都知道Android是一个开放的操作系统,我们可以随心所欲地使用它,所以添加徽章编号的唯一方法 - 使用一些第三方应用程序或自定义启动器,或者前端触摸接口:Samsung TouchWiz或Sony Xperia的界面 . 其他答案使用此功能,您可以在stackoverflow上搜索此功能,例如here . 但是我会再重复一次:这有 no 标准API,我想说这是一个不好的做法 . 应用程序的图标通知徽章是iOS模式,不应该在Android应用程序中使用它 . 在Andrioid中有一个状态栏通知用于这些目的:http://developer.android.com/guide/topics/ui/notifiers/notifications.html因此,如果Facebook或其他人使用它 - 这不是我们应该考虑的常见模式或趋势 . 但是如果你仍然坚持并且不想使用主屏幕小部件,那么请看这里,请:

    How does Facebook add badge numbers on app icon in Android?

    如你所见,这不是一个真正的Facebook应用程序,它是TouchWiz . 在vanilla android中,这可以通过Nova Launcher实现http://forums.androidcentral.com/android-applications/199709-how-guide-global-badge-notifications.html所以,如果您在某处看到图标徽章,请确保它是第3方启动器或触摸界面(前端包装器) . 可能有时Google会将此功能添加到标准Android API中 .

  • 12

    它适用于三星touchwiz Launcher

    public static void setBadge(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);
    }
    
    public static String getLauncherClassName(Context context) {
    
        PackageManager pm = context.getPackageManager();
    
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
    
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                String className = resolveInfo.activityInfo.name;
                return className;
            }
        }
        return null;
    }
    
  • 7

    ShortcutBadger是一个库,它在设备品牌和当前 Launcher 上添加了一个抽象层,并提供了很好的结果 . 适用于LG,索尼,三星,HTC和其他定制 Launcher .

    它甚至可以在Pure Android设备桌面上显示Badge Count .

    更新应用程序图标中的徽章计数就像调用一样简单:

    int badgeCount = 1;
    ShortcutBadger.applyCount(context, badgeCount);
    

    它包括一个演示应用程序,允许您测试其行为 .

  • 74

    我已经弄清楚如何为索尼设备做到这一点 .

    我发表了关于它的博客here . 我还发布了一个关于这个here的单独的问题 .


    Sony设备使用名为 BadgeReciever 的类 .

    • 在清单文件中声明 com.sonyericsson.home.permission.BROADCAST_BADGE 权限:

    • Intent 广播到 BadgeReceiver

    Intent intent = new Intent();
    
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");
    
    sendBroadcast(intent);
    
    • 完成 . 一旦广播 Intent ,启动器应在您的应用程序图标上显示徽章 .

    • 要再次删除徽章,只需发送一个新广播,这次将 SHOW_MESSAGE 设置为false:

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    

    我已经排除了有关我如何找到这个以保持答案简短的细节,但这些都可以在博客中找到 . 对某人来说可能是一本有趣的读物 .

  • 90

    这是在通知启动器图标上显示徽章的示例和最佳方式 .

    在您的应用程序中添加此类

    public class BadgeUtils {
    
        public static void setBadge(Context context, int count) {
            setBadgeSamsung(context, count);
            setBadgeSony(context, count);
        }
    
        public static void clearBadge(Context context) {
            setBadgeSamsung(context, 0);
            clearBadgeSony(context);
        }
    
    
        private static void setBadgeSamsung(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);
        }
    
        private static void setBadgeSony(Context context, int count) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return;
            }
    
            Intent intent = new Intent();
            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.SHOW_MESSAGE", true);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    
            context.sendBroadcast(intent);
        }
    
    
        private static void clearBadgeSony(Context context) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return;
            }
    
            Intent intent = new Intent();
            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.SHOW_MESSAGE", false);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    
            context.sendBroadcast(intent);
        }
    
        private static String getLauncherClassName(Context context) {
    
            PackageManager pm = context.getPackageManager();
    
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
    
            List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
            for (ResolveInfo resolveInfo : resolveInfos) {
                String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
                if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                    String className = resolveInfo.activityInfo.name;
                    return className;
                }
            }
            return null;
        }
    
    
    }
    

    ==> MyGcmListenerService.java通知到来时使用BadgeUtils类 .

    public class MyGcmListenerService extends GcmListenerService { 
    
        private static final String TAG = "MyGcmListenerService"; 
        @Override
        public void onMessageReceived(String from, Bundle data) {
    
                String message = data.getString("Msg");
                String Type = data.getString("Type"); 
                Intent intent = new Intent(this, SplashActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                        PendingIntent.FLAG_ONE_SHOT);
    
                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
                NotificationCompat.BigTextStyle bigTextStyle= new NotificationCompat.BigTextStyle();
    
                bigTextStyle .setBigContentTitle(getString(R.string.app_name))
                        .bigText(message);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(getNotificationIcon())
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(message)
                        .setStyle(bigTextStyle) 
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
    
                int color = getResources().getColor(R.color.appColor);
                notificationBuilder.setColor(color);
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    
                int unOpenCount=AppUtill.getPreferenceInt("NOTICOUNT",this);
                unOpenCount=unOpenCount+1;
    
                AppUtill.savePreferenceLong("NOTICOUNT",unOpenCount,this);  
                notificationManager.notify(unOpenCount /* ID of notification */, notificationBuilder.build()); 
    
    // This is for bladge on home icon          
            BadgeUtils.setBadge(MyGcmListenerService.this,(int)unOpenCount);
    
        }
    
    
        private int getNotificationIcon() {
            boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
            return useWhiteIcon ? R.drawable.notification_small_icon : R.drawable.icon_launcher;
        }
    }
    

    并根据偏好和徽章计数清除通知

    public class SplashActivity extends AppCompatActivity { 
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_splash);
    
                        AppUtill.savePreferenceLong("NOTICOUNT",0,this);
                        BadgeUtils.clearBadge(this);
                }
        }
    
    <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
    

相关问题