如何在app启动器图标中显示通知计数

Waq*_*leh 188 notifications icons android android-layout

三星galaxy note 2 android版4.1.2

我知道之前已经提出过这个问题而且回复是不可能的

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

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

facebook图标

在此输入图像描述

三星galaxy note 2 android版4.1.2

Ole*_*rov 116

安卓("香草"的Android定制无发射器和触摸界面)不会允许应用程序图标的变化,因为它是在密封.apk紧密,一旦程序被编译.没有办法使用标准API以编程方式将其更改为"可绘制".您可以使用小部件而不是图标来实现目标.小部件是可自定义的.请阅读:http://www.cnet.com/8301-19736_1-10278814-251.html和此http://developer.android.com/guide/topics/appwidgets/index.html.另请看这里:https://github.com/jgilfelt/android-viewbadger.它可以帮助你.

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

Facebook如何在Android中的应用程序图标上添加徽章编号?

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


Cha*_*gUZ 92

它适用于三星touchwiz发射器

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;
}
Run Code Online (Sandbox Code Playgroud)

  • @DixitPatel 这很简单。只是“setBadge(上下文,0)” (2认同)
  • 它可以显示一个字符或字符串,例如一个明星'*'而不是一个整数的计数器吗? (2认同)

Ale*_*uti 77

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

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

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

int badgeCount = 1;
ShortcutBadger.applyCount(context, badgeCount);
Run Code Online (Sandbox Code Playgroud)

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

  • Nexus(纯Android)设备仅在桌面上的图标上显示徽章计数 (2认同)
  • 该库在Android 7.0及更高版本中不起作用。我用nexus 5x和nexus 6进行检查。 (2认同)

Mar*_*cus 12

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

我在这里写了博客.我也张贴了关于这一个单独的SO问题在这里.


索尼设备使用一个名为的类BadgeReciever.

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

  2. 广播IntentBadgeReceiver:

    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);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 完成.一旦Intent在广播发射器应显示在您的应用程序图标徽章.

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

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    
    Run Code Online (Sandbox Code Playgroud)

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


Jay*_*hit 8

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

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

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


}
Run Code Online (Sandbox Code Playgroud)

==> 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

 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);
            }
    }
Run Code Online (Sandbox Code Playgroud)
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

303139 次

最近记录:

8 年,3 月 前