首页 > 其他分享 >Android Notification 以及 通知铃音使用

Android Notification 以及 通知铃音使用

时间:2023-12-26 20:23:12浏览次数:27  
标签:PowerManager Notification mp context 铃音 new Android channel

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: 通知铃音

  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;
    }

标签:PowerManager,Notification,mp,context,铃音,new,Android,channel
From: https://www.cnblogs.com/zhjing/p/17929282.html

相关文章

  • Android Vibrator 手机震动
    AndroidVibrator手机震动本篇文章主要讲下手机震动.1:检测是否支持震动/***@paramcontext*@return*是否支持手机震动*/publicstaticbooleanhasVibrator(Contextcontext){ObjectsystemService=context.getSystemService(Context.VIBRATOR_SERVICE);......
  • android-studio-2021.1.1.11-windows 版本遇到的各种大坑
    1、使用2023.12月的最新版本会无法选择java语言开发,所以必须选择较老的版本,比如我选择的android-studiobumbblebee 2021.1.1.11这个版本就可以选择java语言。2、不光需要设置代码自动补全提示,更重要的是最新的SDK,androidapi34有bug,无法语法提示,也可能是不支持我所使用的语......
  • ArgoCD notifications 配置
    ArgoCDnotifications TriggersTriggers定义应发送通知的条件。定义包括名称、条件和通知模板参考。条件是一个谓词表达式,如果通知则返回true应该发送。Triggers列表1.on-created#Application创建2.on-deleted#Applic......
  • Argo Rollouts notifications
    ArgoRolloutsnotificationscm模板argo-rollouts-notification-configmap的configmap需要部署在argo-rollouts名称空间下。apiVersion:v1kind:ConfigMapmetadata:name:argo-rollouts-notification-configmapdata:template.analysis-run-error:|message:Rollo......
  • 40.Android fastbot遍历测试工具
    Fastbot介绍基于model-basedtesting结合机器学习、强化学习的APP稳定性测试工具Fastbotisamodel-basedtestingtoolformodelingGUItransitionstodiscoverappstabilityproblems.Itcombinesmachinelearningandreinforcementlearningtechniquesto......
  • 39.android maxim 遍历测试工具
    maxim介绍 AnefficientAndroidMonkeyTester,availableforemulatorsandrealdevices基于遍历规则的高性能AndroidMonkey,适用于真机/模拟器的APPUI压力测试maximquickstart cdMaximadbpushframework.jar/sdcardadbpushmonkey.jar/sdca......
  • android蓝牙取sbc音频数据
    https://blog.csdn.net/u010481276/article/details/86545272两种方法:方法1:dumpaudio数据Bluedroid协议栈中把bt_target.h中的宏DUMP_PCM_DATA打开,发送到bluetoothHAL层的pcm数据被保存在手机的/data/misc/bluedroid/output_sample.pcm格式。可以用cooleditpro工具分析PCM......
  • Android转车载开发发展前景和待遇怎么样?有前途吗?好找工作吗?
    车载开发发展前景和待遇怎么样?有前途吗?好找工作吗?2023年末还有人提出这样的疑问?话不多说,直接看图今年相较去年市场需求:2023年较2022年同期对比增长229%,2022年较2021年增长了12%。工资待遇:2023年较2022年增长了14%。无论是岗位需求还是薪资待遇都比去年高出一截,而且伴随着新能源汽......
  • 2023年最实用的Android Framework学习路线,让你轻松通过面试和适应实际工作
    许多Android开发者和应聘者都曾反映,在面试或考核过程中,经常遇到与AndroidFramework相关的问题。这些问题常常让他们感到困惑和不安,因为这些问题的确需要深入的理解和扎实的基础。Framework层的原理和机制对于Android开发来说至关重要。从应用启动到用户使用,整个过程中都离不开Fram......
  • Android Audio
    1. AudioFormat定义了一些变量,这些变量在AudioTrack和AudioRecord中用到:一般以下参数ENCODING_PCM_16BIT,ENCODING_PCM_8BIT。代表采样大小android支持的采样大小16bit或者8bit。当然采样大小越大,那么信息量越多,音质也越高,现在主流的采样大小都是16bit,在低质量的语音传输的时候8......