Android通知会不断重复通知声音

时间:2015-10-24 07:23:48

标签: android

以下是我的通知代码代码,但声音不断重复,直到拉下状态栏进行检查。有什么想法吗?

NotificationManager mNotificationManager;
        android.support.v7.app.NotificationCompat.Builder mBuilder;
        Notification mNotification;
        final int NOTIFICATION_ID = 1;

        mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, HomeScreenTabbed.class), 0);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        mBuilder = new android.support.v7.app.NotificationCompat.Builder(ctx);
        mBuilder.setContentTitle(notificationTitle);
        mBuilder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(notificationBody));
        mBuilder.setContentText(notificationBody);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(alarmSound);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher));
        mBuilder.setSmallIcon(R.drawable.ic_pollen_notification);
        mBuilder.setAutoCancel(true);
        mNotification = mBuilder.build();
        // set dismiss on click flags
        mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);

3 个答案:

答案 0 :(得分:7)

我使用mNotification.defaults代替mNotification.flags

解决了这个问题

答案 1 :(得分:1)

尝试将标志FLAG_ONLY_ALERT_ONCE添加到mNotification.flags定义中。

http://developer.android.com/reference/android/app/Notification.html#FLAG_ONLY_ALERT_ONCE

这样可以防止它多次发出声音。

答案 2 :(得分:0)

就是这个
.setOnlyAlertOnce(true)

快乐编码