我无法在主屏幕中显示应用程序图标的通知计数

时间:2015-06-15 05:24:45

标签: android

您好我想在主屏幕上显示应用程序图标的通知计数,但我无法执行此操作。

这是我的代码。

public class MainActivity extends Activity {
   ImageView img;
    BadgeView badge;
    public int badgeCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           badgeCount = 0;
            img = (ImageView)findViewById(R.id.notificationicon);
            Drawable icon;
            try {
                icon = getPackageManager().getApplicationIcon("com.example.notificationapp");
                  img.setImageDrawable(icon);
                    badge = new BadgeView(this, img);
                    badge.setText("10");

            } catch (NameNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            badge = new BadgeView(this, img);
            badge.setText("10");

            badgeCount = Integer.parseInt("5");
            ShortcutBadger.with(getApplicationContext()).count(badgeCount);
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (badge.isShown()) {
                         try {
                                badgeCount = Integer.parseInt("5");
                            } catch (NumberFormatException e) {
                                Toast.makeText(getApplicationContext(), "Error input", Toast.LENGTH_SHORT).show();
                            }

//                              ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
                            ShortcutBadger.with(getApplicationContext()).count(badgeCount);

                            Toast.makeText(getApplicationContext(), "Set count=" + badgeCount, Toast.LENGTH_SHORT).show();
                        badge.decrement(1);
                    } else {
//                      badge.decrement(1);
                        badge.show();
                    }
                }
            });
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
            String currentHomePackage = resolveInfo.activityInfo.packageName;
            ShortcutBadger.with(getApplicationContext()).count(badgeCount);
            TextView textViewHomePackage = (TextView) findViewById(R.id.textViewHomePackage);
            textViewHomePackage.setText("launcher:" + currentHomePackage);
    }

使用这两行代码在app图标上设置通知计数。

ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
                              ShortcutBadger.with(getApplicationContext()).count(badgeCount);

但有了这个,我无法做到这一点。

1 个答案:

答案 0 :(得分:3)

ShortcutBadger仅支持某些第三方发射器。

它不支持许多设备和模拟器上的Android启动器。