You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用被杀死后,如何在收到通知时保持铃声持续播放?

完整实现带铃声的Android通知示例

我看你已经写了通知跳转Intent和铃声播放的基础代码,这里给你补全完整的实现,还优化了几个容易踩坑的细节,让你的通知功能更稳定可靠:

一、完整可运行代码

// 1. 准备跳转的Intent与PendingIntent
Intent i = new Intent(this, NotificationManagerActivity.class);
i.putExtras(bundle);
// 适配API 31+:必须添加FLAG_IMMUTABLE/FLAG_MUTABLE,这里选择FLAG_IMMUTABLE保证安全性
PendingIntent pIntent = PendingIntent.getActivity(
    this, 
    0, 
    i, 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// 2. 播放通知铃声(优化版:增加兜底逻辑+资源释放)
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
    // 若系统无默认闹钟铃声,自动 fallback 到普通通知铃声
    alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}

MediaPlayer mediaPlayer = MediaPlayer.create(this, alarmUri);
mediaPlayer.setLooping(false); // 设置为非循环播放,按需调整
mediaPlayer.start();
// 播放完成后立即释放资源,避免内存泄漏
mediaPlayer.setOnCompletionListener(MediaPlayer::release);

// 3. 构建并发送通知(API 26+ 需配合通知渠道)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "YOUR_CHANNEL_ID")
        .setSmallIcon(R.drawable.ic_notification) // 必须设置的小图标(系统要求)
        .setContentTitle("你的通知标题")
        .setContentText("这里是通知的详细内容")
        .setPriority(NotificationCompat.PRIORITY_HIGH) // 高优先级保证通知弹窗显示
        .setContentIntent(pIntent) // 绑定跳转PendingIntent
        .setAutoCancel(true); // 点击通知后自动取消

// 发送通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1001, builder.build()); // 1001为通知唯一ID

二、关键细节补充

1. 通知渠道创建(API 26+ 必做)

Android 8.0及以上版本必须创建通知渠道,否则通知无法显示。建议在应用启动时执行以下代码:

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "重要通知渠道";
        String channelDesc = "用于展示带有铃声的高优先级通知";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        
        NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID", channelName, importance);
        channel.setDescription(channelDesc);
        // 也可以直接给渠道设置默认铃声,后续该渠道的通知都会自动使用此铃声
        channel.setSound(alarmUri, AudioAttributes.DEFAULT_NOTIFICATION);
        
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

2. 铃声播放的两种方案对比

  • MediaPlayer方案:灵活性高,可控制循环、音量等,但需要手动管理资源释放。
  • Ringtone方案(你注释掉的代码优化版):系统自动管理资源,适合简单播放场景:
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), alarmUri);
    if (ringtone != null) {
        ringtone.play();
    }
    

内容的提问来源于stack exchange,提问作者Mukesh Y.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:38:28