要在Android 11/R中从后台启动前台服务并开始录制音频/视频,可以使用以下解决方案。
首先,在AndroidManifest.xml文件中添加必要的权限和服务声明。请确保包含以下权限:
然后,在AndroidManifest.xml文件中添加服务声明:
接下来,创建一个名为RecordingService的服务类。在此服务类中,我们将使用MediaProjectionManager和MediaRecorder来录制音频/视频。示例代码如下:
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import java.io.IOException;
public class RecordingService extends Service {
private static final String TAG = "RecordingService";
private MediaProjectionManager mMediaProjectionManager;
private MediaProjection mMediaProjection;
private MediaRecorder mMediaRecorder;
private boolean isRecording = false;
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
RecordingService getService() {
return RecordingService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals("START_RECORDING")) {
startForegroundService();
startRecording();
} else if (intent.getAction().equals("STOP_RECORDING")) {
stopRecording();
stopForeground(true);
stopSelf();
}
}
return START_NOT_STICKY;
}
private void startForegroundService() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID")
.setContentTitle("Recording Service")
.setContentText("Recording in progress")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
private void startRecording() {
mMediaProjection = mMediaProjectionManager.getMediaProjection(RESULT_OK, mMediaProjectionCallback);
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile("path_to_save_recording");
try {
mMediaRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed: " + e.getMessage());
}
mMediaRecorder.start();
isRecording = true;
Log.d(TAG, "Recording started");
}
private void stopRecording() {
if (mMediaRecorder != null && isRecording) {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
mMediaProjection.stop();
mMediaProjection = null;
isRecording = false;
Log.d(TAG, "Recording stopped");
}
}
private final MediaProjection.Callback mMediaProjectionCallback = new MediaProjection.Callback() {
@Override
public void onStop() {
stopRecording();
}
};
}
最后,在需要启动录制的地方(例如,闹钟接收器),使用以下代码来启动RecordingService并开始录制:
Intent startIntent = new Intent(context, RecordingService.class);
startIntent.setAction("START_RECORDING");
ContextCompat.startForegroundService(context, startIntent);
要停止录制,使用以下代码:
Intent stopIntent = new Intent(context, RecordingService.class);
stop