即使在杀死app之后,如何在Android收到的通知上保持铃声?

时间:2018-04-21 15:01:34

标签: android android-notifications

我已将通知创建为以下

ntent i = new Intent(this, NotificationManagerActivity.class);
    i.putExtras(bundle);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);

    /*Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();*/
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    MediaPlayer player = MediaPlayer.create(this, notification);
    player.setLooping(true);
    player.start();

    // build notification
    Notification n = new Notification.Builder(this)
            .setContentTitle("Amex Alert")
            .setContentText(messageDetails)
            .setSmallIcon(R.drawable.ic_menu_gallery)
            .setContentIntent(pIntent)
            .setAutoCancel(false)
            .setOngoing(true)
            .addAction(R.drawable.ic_launcher_foreground, "More", pIntent)
            .build();



    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (notificationManager != null) {
        notificationManager.notify(Integer.parseInt(1, n);
    }

通知响铃,当我点击通知时,它会打开NotificationManagerActivity(通知仍然响铃)。当我通过刷卡杀死app时,通知响铃停止。但通知仍然存在,因为我设置了setOngoing(true)(这是必需的)。只有当我停止代码时,通知环才会停止。不是杀了应用程序。

任何想法如何做到这一点?

0 个答案:

没有答案