首页 > 其他分享 >android仿录音机通知

android仿录音机通知

时间:2024-09-25 22:21:56浏览次数:3  
标签:通知 录音 RECORDING private ACTION android 录音机 PendingIntent

在Android中,为了模拟一个录音机应用的通知,你需要创建一个持久的通知,当录音开始时显示出来,并且在录音过程中一直存在,直到录音结束。下面是如何实现这一点的步骤:

1. 创建通知渠道

对于Android Oreo(8.0)及更高版本,你需要创建一个通知渠道。

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "recording_channel";
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);
        mChannel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(mChannel);
    }
}

2. 构建通知

接下来,你需要构建一个Notification对象,它将在录音开始时显示,并且包含一些操作按钮,如暂停和停止录音。

private Notification buildRecordingNotification() {
    Intent stopIntent = new Intent(this, RecordingService.class);
    stopIntent.setAction(ACTION_STOP_RECORDING);
    PendingIntent stopPendingIntent = PendingIntent.getService(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

    Intent pauseIntent = new Intent(this, RecordingService.class);
    pauseIntent.setAction(ACTION_PAUSE_RECORDING);
    PendingIntent pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "recording_channel")
            .setSmallIcon(R.drawable.ic_notification_recording)
            .setContentTitle(getString(R.string.recording_notification_title))
            .setContentText(getString(R.string.recording_notification_text))
            .setOngoing(true)
            .addAction(R.drawable.ic_pause, getString(R.string.pause), pausePendingIntent)
            .addAction(R.drawable.ic_stop, getString(R.string.stop), stopPendingIntent)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    return builder.build();
}

3. 在服务中启动通知

你需要在一个Service中处理录音逻辑,并在录音开始时启动这个通知。

public class RecordingService extends Service {
    private static final String ACTION_START_RECORDING = "com.example.ACTION_START_RECORDING";
    private static final String ACTION_STOP_RECORDING = "com.example.ACTION_STOP_RECORDING";
    private static final String ACTION_PAUSE_RECORDING = "com.example.ACTION_PAUSE_RECORDING";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if (ACTION_START_RECORDING.equals(action)) {
            // 开始录音
            startRecording();

            // 创建通知渠道(如果需要)
            createNotificationChannel();

            // 构建并显示通知
            Notification notification = buildRecordingNotification();
            startForeground(1, notification);
        } else if (ACTION_STOP_RECORDING.equals(action)) {
            // 停止录音
            stopRecording();
        } else if (ACTION_PAUSE_RECORDING.equals(action)) {
            // 暂停录音
            pauseRecording();
        }
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void startRecording() {
        // 实现录音开始的逻辑
    }

    private void stopRecording() {
        // 实现录音停止的逻辑
        // 并且停止前台服务
        stopForeground(true);
        stopSelf();
    }

    private void pauseRecording() {
        // 实现录音暂停的逻辑
    }
}

在这个示例中,RecordingService处理录音的开始、停止和暂停。当录音开始时,它会启动一个前台服务,并显示一个带有暂停和停止按钮的通知。当点击通知上的按钮时,相应的操作会被执行。

请确保替换示例中的占位符(如R.drawable.ic_notification_recording)为你自己的资源标识符,并根据实际需求调整逻辑。此外,startForeground方法的第一个参数是一个唯一的请求码,用来标识这个通知;第二个参数是之前构建的Notification对象。

标签:通知,录音,RECORDING,private,ACTION,android,录音机,PendingIntent
From: https://blog.51cto.com/u_16367370/12112321

相关文章

  • Android 移动应用开发基础案例教程——Activity的跳转
    一、Activity的创建1、创建一个新项目点击Flie--New--NewProject点击EmptyViewsActivity点击Next根据需要可修改项目名称,这里我重命名为CycActivity,然后点击Finish即可完成创建新项目。2、SecondActivity的创建点击java--->com.example.cycactivity,右键new--->A......
  • android 10 禁止通过adb命令的方式删除特定APK
    —a/frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java+++b/frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java@@-1636,84+1636,88@@classPackageManagerShellCommandextends......
  • Android Auto认证是什么?
    AndroidAuto认证是谷歌官方提供的一种认证机制,旨在确保车辆信息娱乐系统与AndroidAuto应用程序接口(API)的兼容性,从而实现与Android设备的无缝连接和通信,为用户提供稳定、安全、高性能的使用体验。AndroidAuto认证目的是确保这些应用程序和系统能够与AndroidAuto平台无缝集成,提供......
  • Android 14.0 recovery竖屏界面旋转为横屏
    1.概述在14.0系统rom项目定制化开发中,由于平板固定横屏显示,而如果recovery界面竖屏显示就觉得怪怪的,所以需要recovery页面横屏显示的功能,所以今天就来解决这个问题2.实现功能相关分析Android的Recovery中,利用bootable\recovery下的minui库作为基础,采用的是直接存取framebu......
  • 关于2024年11月30日PMI认证考试的报名通知
    尊敬的各位考生:经PMI和中国国际人才交流基金会研究决定,中国大陆地区2024年第四期PMI认证考试定于11月30日举办。在基金会网站报名参加本次PMI认证考试的考生须认真阅读下文,知悉考试安排及注意事项,并遵守考试有关规定。一、报名注意事项1.在报名前请考生做好网站用户注册、信息绑定......
  • Android 设备命令行开代理
    #!/bin/bashecho"\n----------------Support------------------"echo"设置默认代理proxyset"echo"设置自定义代理proxyset****:9090"echo"删除代理proxyclean"echo"------------------------------------------......
  • Android USB 整条通知链分析
    以Android13为例:在Android13中,USB插入拔出事件的通知链从硬件层到应用层,依次经过硬件、内核、HAL、Framework、以及应用广播等多个模块,整个流程涉及的具体文件和逻辑如下:硬件层(USBPHY/控制器)文件:/drivers/usb/phy/phy-generic.c(USBPHY驱动)/drivers/usb/dwc3/dwc3-cor......
  • Android Studio制作简单登录界面
    实现目标应用线性布局设计登录界面,要求点击输入学号时弹出数字键盘界面,点击输入密码时弹出字母键盘,出现的文字、数字、尺寸等全部在values文件夹下相应.xml文件中设置好,使用时直接引用。当用户名或密码为空,显示一个提示信息“用户名与密码不能为空!”,当用户名和密码匹配,显示“登录......
  • ASP.NET Core SignalR :学习消息通讯,实现一个消息通知
    ASP.NETCoreSignalR:学习消息通讯,实现一个消息通知  什么是SignalR    目前我用业余时间正在做一个博客系统,其中有个功能就是评论通知,就是假如A用户评论B用户的时候,如果B用户首页处于打开状态,那么就会提示B用户有未读消息。暂时用SignalR来实现这个功能。我也是......
  • Android启动过程深入解析
    Android启动过程深入解析A当按下Android设备电源键时究竟发生了什么?Android的启动过程是怎么样的?A什么是Linux内核?A桌面系统linux内核与Android系统linux内核有什么区别?A什么是引导装载程序?A什么是Zygote?A什么是X86以及ARMlinux?A什么是init.rc?A什么是系统服务?当我们想到......