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;
    }
相关推荐
懒人村杂货铺2 小时前
Android BLE 扫描完整实战
android
TeleostNaCl5 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang95275 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_915918416 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9516 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海7 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿7 小时前
毕业三年后,我离职了
android·面试
编程乐学7 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐8 小时前
Android14 init.rc中on boot阶段操作4
android
fatiaozhang95279 小时前
中国移动中兴云电脑W132D-RK3528-2+32G-刷机固件包(非原机制作)
android·xml·电脑·电视盒子·刷机固件·机顶盒刷机