如何在Android上的通知栏中显示结果

时间:2018-05-30 19:03:40

标签: android

我正在创建一个Android应用程序。我正在寻找的是在计算结果后我希望数值显示在通知窗口上。我该怎么办?

1 个答案:

答案 0 :(得分:0)

要将结果显示在通知中,您可以使用以下代码创建通知:

 Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
            R.drawable.noti_icon);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.noti_icon)
            .setLargeIcon(icon)
            .setContentTitle("Your Title of Notification")
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setContentText("write here your score which you have counted");

    Intent notificationIntent = new Intent(this, YourActivity.class); // here on notification click you are moved on this activity

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(contentIntent);
    Notification notification = notificationBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.visibility |= Notification.VISIBILITY_PUBLIC;


    notificationId = 0;

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, notification);