Android Notification 以及 通知铃音使用

Android Notification 以及 通知铃音使用

上一篇文章讲了手机震动的使用.

本篇继续讲解铃音的使用,并且在讲下通知消息的使用.

1:通知消息的使用

代码如下:

public static void notice(Context context) {
try {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(“Notification test”)
.setContentText(“This is a notification with LED lights”)
.setContentInfo(“This is a notification with LED lights”)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{0, 1000, 500, 2000})
.setLights(Color.RED, 300, 200)// 设置LED灯光效果的颜色、亮起时长和熄灭时长
.setChannelId(context.getPackageName())
.setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
context.getPackageName(),
“Notification msg2”,
NotificationManager.IMPORTANCE_HIGH

);
// 设置通知出现时声音,默认通知是有声音的
channel.setSound(null, null);
channel.enableLights(true);
channel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setVibrationPattern(new long[]{0, 1000, 500, 2000});
notificationManager.createNotificationChannel(channel);
}
new Handler().postDelayed(() -> {
// PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
// @SuppressLint(“InvalidWakeLockTag”) PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
// | PowerManager.ACQUIRE_CAUSES_WAKEUP
// | PowerManager.ON_AFTER_RELEASE,“MyWakeLock”);
// wakeLock.acquire();
notificationManager.notify(2014, builder.build());
},2000);
} catch (Throwable e) {
Log.e(TAG, "noticeLED: ",e );
}
}

Android O以上需要实现NotificationChannel ,并通过createNotificationChannel 设置.

2: 通知铃音

  1. 获取通知铃声
    RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)方法 用于获取当前设备上通知铃声的URI(Uniform Resource Identifier)。该方法的作用是返回一个表示当前设备上默认通知铃声的URI。它接受两个参数:上下文(context)和铃声类型(RingtoneManager.TYPE_NOTIFICATION)。上下文参数用于获取系统服务和资源,而铃声类型参数指定要获取的铃声类型。

public static void noticeVoice(Context context) {
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(context, RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION));
mp.setLooping(false);
mp.setAudioStreamType(getRingType(context));
mp.prepare();
mp.setOnCompletionListener(mp1 -> {
mp1.release(); // 播放完毕后自动释放资源。
});
mp.start();
} catch (Throwable e) {
e.printStackTrace();
}
}

获取铃声类型.:

/**
* 控制当前响铃类型
*/
private static int getRingType(Context context) {
try {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int current = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
switch (current) {
case 0:
return AudioManager.STREAM_RING;
default:
return AudioManager.STREAM_NOTIFICATION;
}
} catch (Throwable e) {
Log.e(TAG, "getRingType: ", e);
}
return AudioManager.STREAM_NOTIFICATION;
}

  • 37
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值