public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
private static final int REQUEST_CODE = 2323;
private TextView mNumberOfNotifications;
private PendingIntent mDeletePendingIntent;
protected static final String ACTION_NOTIFICATION_DELETE
= "com.example.android.activenotifications.delete";
private static final String NOTIFICATION_GROUP =
"com.example.android.activenotifications.notification_type";
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_GROUP_SUMMARY_ID = 1;
private static int sNotificationId = NOTIFICATION_GROUP_SUMMARY_ID + 1;
@Override
protected void onResume() {
super.onResume();
updateNumberOfNotifications();
}
@SuppressLint("StringFormatMatches")
protected void updateNumberOfNotifications() {
final StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();
final int numberOfNotifications = activeNotifications.length;
mNumberOfNotifications.setText(getString(R.string.active_notifications,
numberOfNotifications));
Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications));
}
protected void updateNotificationSummary() {
final StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();
int numberOfNotifications = activeNotifications.length;
for (StatusBarNotification notification : activeNotifications) {
if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) {
numberOfNotifications--;
break;
}
}
if (numberOfNotifications > 1) {
// There are %s ActiveNotifications
String notificationContent = getString(R.string.sample_notification_summary_content,
"" + numberOfNotifications);
final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.setSummaryText(notificationContent))
.setGroup(NOTIFICATION_GROUP)
.setGroupSummary(true);
final Notification notification = builder.build();
mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification);
} else {
mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID);
}
}
private void addNotificationAndUpdateSummaries() {
final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("This is a sample notification")
.setAutoCancel(true)
.setDeleteIntent(mDeletePendingIntent)
.setGroup(NOTIFICATION_GROUP);
final Notification notification = builder.build();
mNotificationManager.notify(getNewNotificationId(), notification);
Log.d(TAG, "Add a notification");
updateNotificationSummary();
updateNumberOfNotifications();
}
public int getNewNotificationId() {
int notificationId = sNotificationId++;
if (notificationId == NOTIFICATION_GROUP_SUMMARY_ID) {
notificationId = sNotificationId++;
}
return notificationId;
}
void init() {
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
mNumberOfNotifications = (TextView) findViewById(R.id.number_of_notifications);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
No comments:
Post a Comment