0
你好我想在主屏幕上的應用程序圖標上顯示通知計數,但我無法做到這一點。我無法在主屏幕上的應用程序圖標上顯示通知計數
這是我的代碼。
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);
}
使用這兩行代碼來設置應用圖標的通知計數。
ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
ShortcutBadger.with(getApplicationContext()).count(badgeCount);
但是,我不能這樣做。
所以我怎麼知道哪個設備支持這個庫,哪個不是? –
請按照ShortcutBadger github項目的鏈接。它在自述https://github.com/leolin310148/ShortcutBadger中解釋 – BrentM