Android Notification 以及 通知铃音使用

Android Notification 以及 通知铃音使用

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

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

1:通知消息的使用

代码如下:

java 复制代码
    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: 通知铃音

java 复制代码
  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;
    }
相关推荐
故渊at5 小时前
系列三:组件化与模块化进阶 | 第11篇 组件化项目规范与问题根治:依赖、资源、Manifest 与混淆的全链路管控
android·架构·mvvm·模块化·组件化
故渊at5 小时前
系列二:MVVM 深度实战与项目重构 | 第7篇 LiveData & StateFlow 状态管理实战:从“粘包弹”到“丝滑流式”
android·重构
是阿建吖!5 小时前
【Linux】信号
android·linux·c语言·c++
alexhilton7 小时前
AppFunctions:让你的Android应用更容易被AI智能体发现
android·kotlin·android jetpack
qq3621967057 小时前
APK文件签名校验教程:验证APK真伪的完整方法
android·智能手机
赏金术士7 小时前
Android 组件化概念和特征
android·kotlin·组件化
2501_9159090613 小时前
深入解析Mock.js:功能、应用及实战案例,提升前端开发效率
android·ios·小程序·https·uni-app·iphone·webview
流星白龙15 小时前
【MySQL高阶】21.撤销表空间,撤销日志
android·mysql·adb
我命由我1234516 小时前
Android 开发,FragmentPagerAdapter 的 isViewFromObject 方法问题
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime