您好我想在主屏幕上显示应用程序图标的通知计数,但我无法执行此操作。
这是我的代码。
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);
但有了这个,我无法做到这一点。